1

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)
hdev
  • 5,399
  • 1
  • 41
  • 55

1 Answers1

1

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

Community
  • 1
  • 1
xmojmr
  • 7,696
  • 5
  • 29
  • 51
  • IMHO It is only a callback and not code injection. It will not run on the server. The server will call the client to invoke his callback. – hdev May 12 '14 at 07:31
  • @user1776231 if you find your design fine and e.g. JSON-RPC useless then my answer is probably not very useful at all. In order to find out what does the 'TypeError: object is not a function' mean and where does it come from, you can use some kind of server-side Node.js debugger and use the break-on-exception feature (IMHO) – xmojmr May 12 '14 at 08:28
  • I'm considering to change my design, but my lack of JS knowledge slows down the progress. – hdev May 12 '14 at 08:57
  • 1
    @user1776231 if you don't require callback here and callback there and just need to send some JSON data here and get some JSON data back then you can use the REST-style API link and design your interfaces and messages without "thinking JavaScript". Just image there are 2 C# end-points exchanging JSON/XML data packets. Client side uses AJAX to make calls to the client. Server usually does not push any data to web clients by itself.. you may utilize http://stackoverflow.com/questions/2353818/how-do-i-get-started-with-node-js – xmojmr May 12 '14 at 10:56