0

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

4

1 回答 1

1

是的,node.js 和 socket.io 支持 Web 套接字。Node.js 需要作为它自己的服务运行,而不是托管在 IIS 中。

如果您想在 IIS 中托管 Web 套接字服务器,您将需要 Windows Server 2012(或 Windows 8)和 IIS 8.0。如本文所述

您可以使用iisnode项目在 IIS 中托管 node.js 应用程序,但同样,一旦您这样做,您将需要 IIS8 来支持 Web 套接字。

于 2013-04-24T05:38:33.223 回答