Question

I want to call a method on a server on which node.js is running.

For this purpose I added SocketIO4Net.Client to my C# project, but I don't know you to pass 'function(callback)' trough the method Emit.

I tried several things, but a I always get the JS Exception 'TypeError: object is not a function'.

JS on server (or here https://github.com/hobbyquaker/ccu.io/blob/master/ccu.io.js#L1756 )

socket.on('getObjects', function(callback) {
    loggerlogger.verbose("socket.io <-- getObjects");
    callback(regaObjects);
});

Code on client

public Task<string> GetObject()
{
    Console.WriteLine("Send GetObject");

    var taskCompletionSource = new TaskCompletionSource<string>();

    dynamic doStuffJs = new Action(() => Console.WriteLine("Hello 0"));
    _client.Emit("getObjects", doStuffJs, null, new Action<Object>(Callback));

    return taskCompletionSource.Task;
}

private void Callback(object o)
{
    Console.WriteLine("Hello 1");
}

Doku for method Emit

/// <summary>
/// <para>Asynchronously sends payload using eventName</para>
/// <para>payload must a string or Json Serializable</para>
/// <para>Mimicks Socket.IO client 'socket.emit('name',payload);' pattern</para>
/// <para>Do not use the reserved socket.io event names: connect, disconnect, open, close, error, retry, reconnect</para>
/// </summary>
/// <param name="eventName"></param>
/// <param name="payload">must be a string or a Json Serializable object</param>
/// <remarks>ArgumentOutOfRangeException will be thrown on reserved event names</remarks>
public void Emit(string eventName, dynamic payload, string endPoint = "", Action<dynamic> callback = null)
Was it helpful?

Solution

If you want to serialize some C# code cross-compile it from .NET to JavaScript, pass it to a JavaScript server as a string inside a JSON data packet then unpack it and run on the server (inject code generated by client (unprotected) to the server processing pipeline) then there is something wrong in the design.

1st obstacle will be that Node.js server understands JavaScript while the code snippet you want to send is written in C#

I recommend you to re-thing the design, inspect some web resources for the typical client-server scenarios.

If you are not happy with REST-style API then you can consider using JSON-RPC.

Some older Stack Overflow discussion about which one is "better" is available at REST vs JSON-RPC?. You can find some of the linked and related questions useful

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top