This project is read-only.

Return Value From Socket.Emit

Mar 13, 2014 at 10:19 AM
Edited Mar 13, 2014 at 10:20 AM
I have the following code:
this.Socket.Emit("GetConfiguration", "Test", null, (callback) =>
            {
                var obj = callback.Args[0];

                JsonConvert.DeserializeObject<PropertyData>(obj);
            });
I wanted the callback to return the deserialized object so that the code returns the deserialized object. Is this possible:
PropertyData testData = this.Socket.Emit("GetConfiguration", "Test", null, (callback) =>
            {
                var obj = callback.Args[0];

               return JsonConvert.DeserializeObject<PropertyData>(obj);
            });
Thanx in advance for the help
Coordinator
Mar 13, 2014 at 6:06 PM
You can, although it's a little hard for me to know exactly what you are really after. Have you tried wrapping the call and using async / await type patterns? This doesn't look to be something specific to SocketIO4net per say, but more of general question on usage & techniques of callbacks in general. If you can give a little more detail on your use-case and maybe what you've tried already and I'll see what I can do to help.

That said, StackOverflow is probably the best bet for any general/advanced .Net type of questions...

Something (untested) that might get you started however:
public Task<string> RemoteConfiguration(string payload )
        {
            var taskCompletionSource = new TaskCompletionSource<string>();
            socket.Emit("GetConfiguration", new { settings = payload }, null,
                (callback) =>
                {
                    var jsonMsg = callback as JsonEncodedEventMessage; // callback will be of type JsonEncodedEventMessage, cast for intellisense
                    taskCompletionSource.SetResult(jsonMsg.ToJsonString());
                });
            return taskCompletionSource.Task;
        }

        public async void GetConfiguration()
        {
            var testData = await RemoteConfiguration("abcdefg");
            // ...continue with some other work ...
        }
Jim
Mar 14, 2014 at 9:22 AM
Jim

Thanx a lot for your help. I implemented your solution and it does what I want it to do thanks.