我有一个像bellow一样的websocket服务器应用程序。 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);
};
});
}
这是一个Console C#应用程序,我有一个Timer对象,它调用_socket.Send(value.ToString());对于所有连接的客户端,每一秒钟。
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());
}
}
}
我有像下面这样的HTML客户端。其中status是一个标签,用于显示服务器的值。
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>
每一件事情都适用于MultiClient场景.... 现在我只想托管服务器并希望从任何地方访问.. 目前我正在使用.Net 4.0。 我开始知道有一个规定是.net 4.5到主机WCF服务与websocket功能。我不能使用.net 4.5。和IIS8太...... :( 有没有选择来托管我的服务器。??????? node.js ????
的任何选项谢谢, Arijit