This project is read-only.

Socket.IO has the option of restricting portions of your connection and events to namespaces.  This is not the same as rooms or channels (Join / leave of rooms has been much more useful to me then namespaces, but hey, every environment has different needs).   The javascript sample might look like this:

<script>
    var chat = io.connect('http://localhost:3000/chat')
    , logger = io.connect('http://localhost:3000/logger);

    chat.on('connect', function () {
        chat.emit('hi!');
    });

    logger.on('traceEvent', function () {
        news.emit('woot');
    });
</script>

In a similar style – you can use namespaces in one of three ways.

Direct Connection

You can simply append the namespace to the end of the url, and that becomes the default namespace.

void direct_Example()
{
    var directSocket = new Client("http://127.0.0.1:3000/logger"); // url to the nodejs
    directSocket.Connect();
    directSocket.On("connect", (fn) =>
    {
        Console.WriteLine("\r\nConnected event...\r\n");
    });

    directSocket.On("traceEvent", (eventLog) =>
        {
            // do something with eventLog
        });

    directSocket.Emit("messageAck", new { hello = "papa" });
}

Compatible Syntax Style

With this style, you can mix and match a root namespace (default) with other defined namespaces, keeping a similar syntax style to javascript. 

void similar_Example()
{
    var socket = new Client("http://127.0.0.1:3000/"); // url to the nodejs
    socket.Connect();
    socket.On("connect", (fn) =>
    {
        Console.WriteLine("\r\nConnected event...\r\n");
    });

    var logger = socket.Connect("/logger"); // connect to the logger ns
    logger.On("traceEvent", (eventLog) =>
    {
        // do something with eventLog
    });
}

Explicit Endpoint

Socket Emit and On event registration have overloads for endpoint definitions – simply provide the desired namespace (endpoint).

void explicit_Example()
{
    var socket = new Client("http://127.0.0.1:3000/"); // url to the nodejs
    socket.Connect();
    socket.Connect("/logger");

    // EventMessage by namespace
    socket.On("traceEvent", "/logger", (eventLog) =>
    {
        Console.WriteLine("recv #1 [logger].[traceEvent] : {0}\r\n", eventLog.Json.GetFirstArgAs<EventLog>().ToJsonString());
    });
    socket.Emit("messageAck", new { hello = "papa" }, "/logger");
}

Last edited Feb 7, 2012 at 4:44 AM by jstott, version 3

Comments

No comments yet.