I have a websocket server application like the bellow. private static void InitializeSockets() { _sockets = new List();
var server = new WebSocketServer("ws://localhost:1234");
server.Start(socket =>
{
socket.OnOpen = () =>
{
Console.WriteLine("Socket is Opened..timeStame: " + DateTime.Now);
Console.WriteLine("Connected to " + socket.ConnectionInfo.ClientIpAddress);
_sockets.Add(socket);
};
socket.OnClose = () =>
{
Console.WriteLine("Socket is Closed..timeStame:" + DateTime.Now);
Console.WriteLine("Disconnected from " + socket.ConnectionInfo.ClientIpAddress);
_sockets.Remove(socket);
};
socket.OnMessage = message =>
{
Console.WriteLine(socket.ConnectionInfo.ClientIpAddress + " Message: "
+ message + " timeStame: " + DateTime.Now);
};
});
}
which is a Console C# Application and I have a Timer Object which is calling _socket.Send(value.ToString()); for all the connected client in every second.
static void timer_Elapsed(object sender, ElapsedEventArgs e)
{
Random rand = new Random();
int value = rand.Next(0, 100);
if (_sockets.Count > 0)
{
foreach (var _socket in _sockets)
{
_socket.Send(value.ToString());
}
}
}
And I have HTML client like the bellow. where status is a label to display the value from the server.
function socketSetup () {
if (typeof (WebSocket) !== 'undefined') {
var status = document.getElementById('status');
status.innerHTML = "Connecting to server...";
socket = new WebSocket('ws://localhost:1234');
socket.onopen = function () {
console.log("onopen is Called..");
status.innerHTML = "Connection successful.";
};
socket.onclose = function () {
console.log("onclose is Called..");
status.innerHTML = "Connection closed.";
};
socket.onmessage = function (e) {
console.log("onmessage is Called..");
var jsonObject = eval('(' + e.data + ')');
status.innerHTML = jsonObject;
socket.send("Client Updated With :" + jsonObject);
};
} else
alert("Your Browser does not support Web Socket");
};
</script>
Every thing is working fine with MultiClient scenario too.... Now I just Want to host the Server and want to access from the anywhere.. Currently I am using .Net 4.0. I came to know that there is a provision is .net 4.5 to Host WCF service with websocket feature. I can't use .net 4.5. and IIS8 too... :( Is there any option to host my server.??????? any option with node.js????
Thanks, Arijit