.net - return from socketIO in C# project -


i have code receive data server. log inside socket.on give correct data give 0 @ return. , can not put return socket.on.

public int getinfor(string userid) {     int result = 0;     socket.on("data" , (socketioevent e) => {         formdata data= jss.deserialize<formdata>(string.format("{0}", e.data));         if (data.err)              result = int32.parse(data.infor);         else result = -1;         debug.log (result);     });     debug.log (result);     return result; } 

i think happens, because method returns result set 0 straightaway without waiting receive event.

here's simple solution:

public void getinfor(string userid, action<int> resultaction) {     int result = 0;     socket.on("data" , (socketioevent e) => {         formdata data= jss.deserialize<formdata>(string.format("{0}", e.data));         if (data.err)              result = int32.parse(data.infor);         else              result = -1;         resultaction.invoke(result); // or simplt resultaction(result);     }); } 

usage

this.getinfor("exampleuserid", result => {     // result }); 

Comments