|
| 1 | +# Socket.IO .NET |
| 2 | + |
| 3 | +Socket.IO enables real-time bidirectional event-based communication. |
| 4 | +It works on every platform, browser or device, focusing equally on reliability and speed. |
| 5 | + |
| 6 | + * **Real-time analytics** |
| 7 | + Push data to clients that gets represented as real-time counters, charts or logs. |
| 8 | + * **Instant messaging and chat** |
| 9 | + Socket.IO's "Hello world" is a chat app in just a few lines of code. |
| 10 | + * **Binary streaming** |
| 11 | + Starting in 1.0, it's possible to send any blob back and forth: image, audio, video. |
| 12 | + * **Document collaboration** |
| 13 | + Allow users to concurrently edit a document and see each other's changes. |
| 14 | + |
| 15 | +> **USED BY EVERYONE** |
| 16 | +> From Microsoft Office, Yammer, Zendesk, Trello... to hackathon winners and little startups. |
| 17 | +
|
| 18 | +> **IMMENSELY POWERFUL, YET EASY TO USE** |
| 19 | +> Our getting started guide will show you how to create lots of amazing applications in fewer |
| 20 | +> than 200 lines of code. |
| 21 | +
|
| 22 | +## Connect to Server |
| 23 | + |
| 24 | +Connecting to a Socket.IO server is just two lines: |
| 25 | + |
| 26 | + // connect to a Socket.IO server |
| 27 | + socket = IO.Socket("http://chat.socket.io/"); |
| 28 | + socket.Connect(); |
| 29 | + |
| 30 | + // disconnect from the server |
| 31 | + socket.Close(); |
| 32 | + |
| 33 | +## Subscribe to Events |
| 34 | + |
| 35 | +Listening for messages from the server is easy,all we need to do is |
| 36 | +attach a delegate to the event name using the `On` method. |
| 37 | + |
| 38 | +The `data` received from the http://chat.socket.io/ server is a `JToken` value: |
| 39 | + |
| 40 | + // whenever the server emits "login", print the login message |
| 41 | + socket.On("login", data => { |
| 42 | + connected = true; |
| 43 | + |
| 44 | + // get the json data from the server message |
| 45 | + var jobject = data as JToken; |
| 46 | + |
| 47 | + // get the number of users |
| 48 | + var numUsers = jobject.Value<int>("numUsers"); |
| 49 | + |
| 50 | + // display the welcome message... |
| 51 | + }); |
| 52 | + |
| 53 | + // whenever the server emits "new message", update the chat body |
| 54 | + socket.On("new message", data => { |
| 55 | + // get the json data from the server message |
| 56 | + var jobject = data as JToken; |
| 57 | + |
| 58 | + // get the message data values |
| 59 | + var username = jobject.Value<string>("username"); |
| 60 | + var message = jobject.Value<string>("message"); |
| 61 | + |
| 62 | + // display message... |
| 63 | + }); |
| 64 | + |
| 65 | +## Send a Message |
| 66 | + |
| 67 | +Sending a message to the server is just a single line of code that makes use of the |
| 68 | +`Emit` method: |
| 69 | + |
| 70 | + // we can send messages to the server |
| 71 | + socket.Emit("add user", "username"); |
| 72 | + if (connected) { |
| 73 | + socket.Emit("new message", "This is a message from Xamarin.Android..."); |
| 74 | + } |
| 75 | + |
| 76 | + // or we can just send events |
| 77 | + socket.Emit("typing"); |
| 78 | + // cancel that typing event |
| 79 | + socket.Emit("stop typing"); |
0 commit comments