Files

80 lines
2.6 KiB
Markdown

# Socket.IO .NET
Socket.IO enables real-time bidirectional event-based communication.
It works on every platform, browser or device, focusing equally on reliability and speed.
* **Real-time analytics**
Push data to clients that gets represented as real-time counters, charts or logs.
* **Instant messaging and chat**
Socket.IO's "Hello world" is a chat app in just a few lines of code.
* **Binary streaming**
Starting in 1.0, it's possible to send any blob back and forth: image, audio, video.
* **Document collaboration**
Allow users to concurrently edit a document and see each other's changes.
> **USED BY EVERYONE**
> From Microsoft Office, Yammer, Zendesk, Trello... to hackathon winners and little startups.
> **IMMENSELY POWERFUL, YET EASY TO USE**
> Our getting started guide will show you how to create lots of amazing applications in fewer
> than 200 lines of code.
## Connect to Server
Connecting to a Socket.IO server is just two lines:
// connect to a Socket.IO server
socket = IO.Socket("http://chat.socket.io/");
socket.Connect();
// disconnect from the server
socket.Close();
## Subscribe to Events
Listening for messages from the server is easy,all we need to do is
attach a delegate to the event name using the `On` method.
The `data` received from the http://chat.socket.io/ server is a `JToken` value:
// whenever the server emits "login", print the login message
socket.On("login", data => {
connected = true;
// get the json data from the server message
var jobject = data as JToken;
// get the number of users
var numUsers = jobject.Value<int>("numUsers");
// display the welcome message...
});
// whenever the server emits "new message", update the chat body
socket.On("new message", data => {
// get the json data from the server message
var jobject = data as JToken;
// get the message data values
var username = jobject.Value<string>("username");
var message = jobject.Value<string>("message");
// display message...
});
## Send a Message
Sending a message to the server is just a single line of code that makes use of the
`Emit` method:
// we can send messages to the server
socket.Emit("add user", "username");
if (connected) {
socket.Emit("new message", "This is a message from Xamarin.Android...");
}
// or we can just send events
socket.Emit("typing");
// cancel that typing event
socket.Emit("stop typing");