SocketIO4Net.Client

NuGet updated to v06.26

Project Description

SocketIO4Net.Client provides a .NET 4.0 C# client for Socket.IO.  It provides an overall interface similar to the client JavaScript experience, leveraging the WebSocket4Net project for an underlying websocket implementation.

My goal for this project is a simple & familiar experience for .net clients.  You know you want your .Net app to join in some of the fun, right?  Besides, all the cool kids are using Nodejs and Socket.IO these days anyway, give it a whirl.

This resulting signature is very similar to the socket.io javascript counterpart:

node.js / JavaScript client

socket.on('news', function (data) {
    console.log(data);
});

C# .net client

socket.On("news", (data) =>    {
    Console.WriteLine(data);
});

The all important - Sample / Demo code snippet 

Client socket;
public void Execute()
{
    Console.WriteLine("Starting TestSocketIOClient Example...");

    socket = new Client("http://127.0.0.1:3000/"); // url to nodejs 
    socket.Opened += SocketOpened;
    socket.Message += SocketMessage;
    socket.SocketConnectionClosed += SocketConnectionClosed;
    socket.Error += SocketError;
            
    // register for 'connect' event with io server
    socket.On("connect", (fn) =>
    {
        Console.WriteLine("\r\nConnected event...\r\n");
        Console.WriteLine("Emit Part object");

        // emit Json Serializable object, anonymous types, or strings
        Part newPart = new Part() 
        { PartNumber = "K4P2G324EC", Code = "DDR2", Level = 1 };
        socket.Emit("partInfo", newPart);
    });

    // register for 'update' events - message is a json 'Part' object
    socket.On("update", (data) =>
    {
        Console.WriteLine("recv [socket].[update] event");
        //Console.WriteLine("  raw message:      {0}", data.RawMessage);
        //Console.WriteLine("  string message:   {0}", data.MessageText);
        //Console.WriteLine("  json data string: {0}", data.Json.ToJsonString());
        //Console.WriteLine("  json raw:         {0}", data.Json.Args[0]);
                
        // cast message as Part - use type cast helper
        Part part = data.Json.GetFirstArgAs<Part>();
        Console.WriteLine(" Part Level:   {0}\r\n", part.Level);
    });

    // make the socket.io connection
    socket.Connect();
}

Getting Started

Two ways to get started:

  1. Download source code, compile, and run the included Test & Sample code and have a look around.
  2. Install via Nuget – run the following command in the Package Manager Console >  Install-Package SocketIO4Net.Client

See the Documentation section for further details.

Suggestions

If you have any suggestions for this project, please leave a post in the Discussions on this page!

PayPal - The safer, easier way to pay online!

Project Dependencies 

WebSocket4net by kerryjiang  WebSocket4Net provides a full websocket client implementation (the core websocket implementation ) 
Json.Net by James Newton-King Json.NET is a popular high-performance JSON framework for .NET

Last edited Jul 20, 2012 at 3:51 PM by jstott, version 46