|
1 | | -socketio4net |
2 | | -============ |
3 | | - |
4 | | -SocketIO4Net provides a .NET v4.0 & v4.5 (C#) client for Socket.IO. |
| 1 | +socketio4net |
| 2 | +============ |
| 3 | + |
| 4 | +SocketIO4Net.Client provides a .NET 4.0 & v4.5 (C#) client for Socket.IO. It provides an overall interface similar to a client JavaScript experience, leveraging the WebSocket4Net project for an underlying websocket implementation. |
| 5 | + |
| 6 | +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. |
| 7 | + |
| 8 | +## Development Branch |
| 9 | + |
| 10 | +The Development branch contains wip code that incorporates xhr-polling in addition to websocket support. A new webserver & client dashboard has also been added into the samples in samples/node.sever. |
| 11 | + |
| 12 | +From VS2012/VS2013, compile and start the the Samples/Console_Events project after you have started the samples/node.server. |
| 13 | + |
| 14 | +*In order to run the samples/node.server dashboard - you will need to run `npm install` in that directory initially.* |
| 15 | + |
| 16 | +* `install.cmd` will run npm install for you |
| 17 | +* `startServer.cmd` will then run the nodejs server, and launch a chrome browser window to http://localhost:3000 |
| 18 | +* `debugServer.cmd` will run node-inspector and launch chrome browser and allow you to run a debug session against the server |
| 19 | + |
| 20 | +* At this time, Nuget packages have not yet been updated |
| 21 | + |
| 22 | +This resulting signature is very similar to the socket.io javascript counterpart: |
| 23 | + |
| 24 | +## node.js / JavaScript client |
| 25 | +```` |
| 26 | +socket.on('news', function (data) { |
| 27 | + console.log(data); |
| 28 | +}); |
| 29 | +```` |
| 30 | +## C# .net client |
| 31 | +```` |
| 32 | +socket.On("news", (data) => { |
| 33 | + Console.WriteLine(data); |
| 34 | +}); |
| 35 | +```` |
| 36 | +The all important - Sample / Demo code snippet |
| 37 | + |
| 38 | +Client socket; |
| 39 | +public void Execute() |
| 40 | +{ |
| 41 | + Console.WriteLine("Starting TestSocketIOClient Example..."); |
| 42 | + |
| 43 | + socket = new Client("http://127.0.0.1:3000/"); // url to nodejs |
| 44 | + socket.Opened += SocketOpened; |
| 45 | + socket.Message += SocketMessage; |
| 46 | + socket.SocketConnectionClosed += SocketConnectionClosed; |
| 47 | + socket.Error += SocketError; |
| 48 | + |
| 49 | + // register for 'connect' event with io server |
| 50 | + socket.On("connect", (fn) => |
| 51 | + { |
| 52 | + Console.WriteLine("\r\nConnected event...\r\n"); |
| 53 | + Console.WriteLine("Emit Part object"); |
| 54 | + |
| 55 | + // emit Json Serializable object, anonymous types, or strings |
| 56 | + Part newPart = new Part() |
| 57 | + { PartNumber = "K4P2G324EC", Code = "DDR2", Level = 1 }; |
| 58 | + socket.Emit("partInfo", newPart); |
| 59 | + }); |
| 60 | + |
| 61 | + // register for 'update' events - message is a json 'Part' object |
| 62 | + socket.On("update", (data) => |
| 63 | + { |
| 64 | + Console.WriteLine("recv [socket].[update] event"); |
| 65 | + //Console.WriteLine(" raw message: {0}", data.RawMessage); |
| 66 | + //Console.WriteLine(" string message: {0}", data.MessageText); |
| 67 | + //Console.WriteLine(" json data string: {0}", data.Json.ToJsonString()); |
| 68 | + //Console.WriteLine(" json raw: {0}", data.Json.Args[0]); |
| 69 | + |
| 70 | + // cast message as Part - use type cast helper |
| 71 | + Part part = data.Json.GetFirstArgAs<Part>(); |
| 72 | + Console.WriteLine(" Part Level: {0}\r\n", part.Level); |
| 73 | + }); |
| 74 | + |
| 75 | + // make the socket.io connection |
| 76 | + socket.Connect(); |
| 77 | +} |
| 78 | + |
0 commit comments