Forked implementation of the Socket.IO client for C# using WebSocket4Net.
var io = require("socket.io").listen(3000);
io.sockets.on("connection", function (socket) {
socket.on("data", function (data) {
console.log("Client sent: " + data);
if (data) {
socket.emit("data", data.toUpperCase(), { length: data.length });
}
});
});
Start the server by running npm install
then node index.js
from the Example/Server
directory.
// Create client instance
var io = new SocketIOClient();
// Client connected
io.ConnectionEstablished += socket => {
MessageBox.Show("Connection established");
// Setup event handler
socket.On("data", (data, callback) =>
Dispatcher.BeginInvoke(() => {
foreach (var datum in data)
MessageBox.Show(String.Format("Server sent: {0}", datum.ToString()));
})
);
// Emit event
socket.Emit("data", "Hello, World!");
};
// Client disconnected
io.ConnectionFailed += () =>
Dispatcher.BeginInvoke(() => MessageBox.Show("Connection lost!"));
// Connect
io.Connect("http://192.168.0.100:8080");
Put this code inside page constructor.