C#作为Websocket服务器,用于HTML5 Websocket连接。

4

我想现在是时候询问别人了。是否有可能使用C#创建Websocket服务器,并使用HTML5代码进行服务器请求?

我目前正在使用System包来处理Websocket。我下载了一段代码,这是它:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WebSocketChatServer
{
    using WebSocketServer;

    class ChatServer
    {
        WebSocketServer wss;
        List<User> Users = new List<User>();
        string unknownName = "john doe";


        public ChatServer()
        {

            // wss = new WebSocketServer(8181, "http://localhost:8080", "ws://localhost:8181/chat");

            wss = new WebSocketServer(8080, "http://localhost:8080", "ws://localhost:8080/dotnet/Chats");


            wss.Logger = Console.Out;
            wss.LogLevel = ServerLogLevel.Subtle;
            wss.ClientConnected += new ClientConnectedEventHandler(OnClientConnected);
            wss.Start();
            KeepAlive();




        }

        private void KeepAlive()
        {
            string r = Console.ReadLine();
            while (r != "quit")
            {
                if(r == "users")
                {
                    Console.WriteLine(Users.Count);
                }
                r = Console.ReadLine();
            }
        }



        void OnClientConnected(WebSocketConnection sender, EventArgs e)
        {
            Console.WriteLine("test");
            Users.Add(new User() { Connection = sender });
            sender.Disconnected += new WebSocketDisconnectedEventHandler(OnClientDisconnected);
            sender.DataReceived += new DataReceivedEventHandler(OnClientMessage);

        }

        void OnClientMessage(WebSocketConnection sender, DataReceivedEventArgs e)
        {
            Console.WriteLine(sender);
            User user = Users.Single(a => a.Connection == sender);
            if (e.Data.Contains("/nick"))
            {
                string[] tmpArray = e.Data.Split(new char[] { ' ' });
                if (tmpArray.Length > 1)
                {
                    string myNewName = tmpArray[1];
                    while (Users.Where(a => a.Name == myNewName).Count() != 0)
                    {
                        myNewName += "_";
                    }
                    if (user.Name != null)
                        wss.SendToAll("server: '" + user.Name + "' changed name to '" + myNewName + "'");
                    else
                        sender.Send("you are now know as '" + myNewName + "'");
                    user.Name = myNewName;
                }
            }
            else
            {
                string name = (user.Name == null) ? unknownName : user.Name;
                wss.SendToAllExceptOne(name + ": " + e.Data, sender);
                sender.Send("me: " + e.Data);
            }
        }

        void OnClientDisconnected(WebSocketConnection sender, EventArgs e)
        {
            try
            {

                User user = Users.Single(a => a.Connection == sender);
                string name = (user.Name == null) ? unknownName : user.Name;
                wss.SendToAll("server: "+name + " disconnected");
                Users.Remove(user);
            }
            catch (Exception exc)
            {

                Console.WriteLine("ehm...");
            }

        }
    }
}

我有这段客户端代码:

<!HTML>
    <head>

        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
        </script>

        <script language="javascript" type="text/javascript">  

        jQuery(document).ready(function(){
            var socket = new WebSocket("ws://localhost:8080");  
            socket.onopen = function(){  
                alert("Socket has been opened!");  
            }  
        });
        </script>  
    </head>
</HTML>

当我运行C#控制台应用程序并加载客户端页面时,应用程序告诉我有人连接到它正在侦听的端口。但是在客户端中,当我查看firebug的控制台时,它给出了初学者经典错误:
Firefox can't establish a connection to the server at ws://localhost:8080/

我希望能够先建立成功的WebSocket连接,然后从服务器向客户端推送价值。我考虑过Alchemy,但我拥有的版本是Visual Studio Express 2010,这是免费版,并且它说“此应用程序版本不支持解决方案文件夹”。 如有任何帮助,将不胜感激。
6个回答

3

我已经开发了一个JavaScript/HTML5游戏的服务器大约7个月了,你有考虑过使用Alchemy Websockets吗?它非常容易使用。

示例:

using Alchemy;
using Alchemy.Classes;
namespace GameServer
{
    static class Program
    {
        public static readonly ConcurrentDictionary<ClientPeer, bool> OnlineUsers = new ConcurrentDictionary<ClientPeer, bool>();
        static void Main(string[] args)
        {
            var aServer = new WebSocketServer(4530, IPAddress.Any)
            {
                OnReceive = context => OnReceive(context),
                OnConnected = context => OnConnect(context),
                OnDisconnect = context => OnDisconnect(context),
                TimeOut = new TimeSpan(0, 10, 0),
                FlashAccessPolicyEnabled = true
            };
        }
        private static void OnConnect(UserContext context)
        {
            var client = new ClientPeer(context);
            OnlineUsers.TryAdd(client, false);
            //Do something with the new client
        }
    }
}

你可以看到它非常易于使用,而且我认为他们的文档非常好(注意ClientPeer是我的自定义类,这里只是以它作为示例)。


1
你所试图实现的目标将会更容易,如果你看一下ASP.NET SignalR。它支持高级中心来实现实时通信,并且还有一个持久连接低级类来对通信进行细粒度控制。支持多个客户端类型和回退,如果通信两端不支持WebSockets(它可以选择使用长轮询或Forever Frames)。

1
但要求使用HTML5和C#。 - Charmie
请查看https://dev59.com/3Ggu5IYBdhLWcg3ws5BC - SignalR独立于ASP.Net并且可以使用WebSockets。 - weismat
1
@weismat SignalR是asp.net的一部分..它不是独立的。请查看此链接http://www.asp.net/signalr - scartag
1
请查看BasicChat.SelfHost示例——无需ASP.Net参考。https://github.com/SignalR/Samples/tree/master/BasicChat.SelfHost - weismat

1
这个错误的原因(很可能)是您没有响应握手。一旦连接建立,浏览器会发送一些数据,服务器必须做出适当的响应(否则浏览器将关闭连接)。您可以在wikispecification上了解更多相关信息。

0
只需将解决方案中WebSocketServer.cs文件中的shakehands更改为下面的代码,您的错误就会消失。
private void ShakeHands(Socket conn)
    {
        using (var stream = new NetworkStream(conn))
        using (var reader = new StreamReader(stream))
        using (var writer = new StreamWriter(stream))
        {
            //read handshake from client (no need to actually read it, we know its there):
            LogLine("Reading client handshake:", ServerLogLevel.Verbose);
            string r = null;
            Dictionary<string, string> headers = new Dictionary<string, string>();
            while (r != "")
            {
                r = reader.ReadLine();
                string[] tokens = r.Split(new char[] { ':' }, 2);
                if (!string.IsNullOrWhiteSpace(r) && tokens.Length > 1)
                {
                    headers[tokens[0]] = tokens[1].Trim();
                }
                LogLine(r, ServerLogLevel.Verbose);
            }


            //string line = string.Empty;
            //while ((line = reader.ReadLine()) != string.Empty)
            //{
            //    string[] tokens = line.Split(new char[] { ':' }, 2);
            //    if (!string.IsNullOrWhiteSpace(line) && tokens.Length > 1)
            //    {
            //        headers[tokens[0]] = tokens[1].Trim();
            //    }
            //}

            string responseKey = "";
            string key = string.Concat(headers["Sec-WebSocket-Key"], "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
            using (SHA1 sha1 = SHA1.Create())
            {
                byte[] hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(key));
                responseKey = Convert.ToBase64String(hash);
            }



            // send handshake to the client
            writer.WriteLine("HTTP/1.1 101 Web Socket Protocol Handshake");
            writer.WriteLine("Upgrade: WebSocket");
            writer.WriteLine("Connection: Upgrade");
            writer.WriteLine("WebSocket-Origin: " + webSocketOrigin);
            writer.WriteLine("WebSocket-Location: " + webSocketLocation);
            //writer.WriteLine("Sec-WebSocket-Protocol: chat");
            writer.WriteLine("Sec-WebSocket-Accept: " + responseKey);
            writer.WriteLine("");
        }


        // tell the nerds whats going on
        LogLine("Sending handshake:", ServerLogLevel.Verbose);
        LogLine("HTTP/1.1 101 Web Socket Protocol Handshake", ServerLogLevel.Verbose);
        LogLine("Upgrade: WebSocket", ServerLogLevel.Verbose);
        LogLine("Connection: Upgrade", ServerLogLevel.Verbose);
        LogLine("WebSocket-Origin: " + webSocketOrigin, ServerLogLevel.Verbose);
        LogLine("WebSocket-Location: " + webSocketLocation, ServerLogLevel.Verbose);
        LogLine("", ServerLogLevel.Verbose);

        LogLine("Started listening to client", ServerLogLevel.Verbose);
        //conn.Listen();
    }

0
您还可以查看WebSocketRPC库,它应该很容易使用,无论是“原始”连接还是RPC连接(消息也可以混合使用)。
以下内容可能对您有用:
负责发送/接收原始消息的代码位于Connection class中。 在存储库中,您还可以找到如何实现基本JavaScript客户端的方法。
免责声明:我是作者。

0

我修改了一段我在网上下载的代码,现在这是我的成果:

using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Security.Cryptography;
using System.Threading;




namespace WebSocketServer
{
    public enum ServerLogLevel { Nothing, Subtle, Verbose };
    public delegate void ClientConnectedEventHandler(WebSocketConnection sender, EventArgs e);

    public class WebSocketServer
    {
        #region private members
        private string webSocketOrigin;     // location for the protocol handshake
        private string webSocketLocation;   // location for the protocol handshake
        #endregion
        static private string guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
        static IPEndPoint ipLocal;

        public event ClientConnectedEventHandler ClientConnected;

        /// <summary>
        /// TextWriter used for logging
        /// </summary>
        public TextWriter Logger { get; set; }     // stream used for logging

        /// <summary>
        /// How much information do you want, the server to post to the stream
        /// </summary>
        public ServerLogLevel LogLevel = ServerLogLevel.Subtle;

        /// <summary>
        /// Gets the connections of the server
        /// </summary>
        public List<WebSocketConnection> Connections { get; private set; }

        /// <summary>
        /// Gets the listener socket. This socket is used to listen for new client connections
        /// </summary>
        public Socket ListenerSocker { get; private set; }

        /// <summary>
        /// Get the port of the server
        /// </summary>
        public int Port { get; private set; }


        public WebSocketServer(int port, string origin, string location)
        {
            Port = port;
            Connections = new List<WebSocketConnection>();
            webSocketOrigin = origin;
            webSocketLocation = location;
        }

        /// <summary>
        /// Starts the server - making it listen for connections
        /// </summary>
        public void Start()
        {
            // create the main server socket, bind it to the local ip address and start listening for clients
            ListenerSocker = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            ipLocal = new IPEndPoint(IPAddress.Loopback, Port);
            ListenerSocker.Bind(ipLocal);
            ListenerSocker.Listen(100);



            LogLine(DateTime.Now + "> server stated on " + ListenerSocker.LocalEndPoint, ServerLogLevel.Subtle);

            ListenForClients();
        }

        // look for connecting clients
        private void ListenForClients()
        {
            ListenerSocker.BeginAccept(new AsyncCallback(OnClientConnect), null);
        }

        private void OnClientConnect(IAsyncResult asyn)
        {

            byte[] buffer = new byte[1024];
            string headerResponse = "";

            // create a new socket for the connection
            var clientSocket = ListenerSocker.EndAccept(asyn);
            var i = clientSocket.Receive(buffer);
            headerResponse = (System.Text.Encoding.UTF8.GetString(buffer)).Substring(0, i);
            //Console.WriteLine(headerResponse);


            if (clientSocket != null)
            {

                // Console.WriteLine("HEADER RESPONSE:"+headerResponse);
                var key = headerResponse.Replace("ey:", "`")
                              .Split('`')[1]                     // dGhlIHNhbXBsZSBub25jZQ== \r\n .......
                              .Replace("\r", "").Split('\n')[0]  // dGhlIHNhbXBsZSBub25jZQ==
                              .Trim();
                var test1 = AcceptKey(ref key);
                var newLine = "\r\n";
                var name = "Charmaine";
                var response = "HTTP/1.1 101 Switching Protocols" + newLine
                         + "Upgrade: websocket" + newLine
                         + "Connection: Upgrade" + newLine
                         + "Sec-WebSocket-Accept: " + test1 + newLine + newLine
                         + "Testing lang naman po:" + name
                         ;

                // which one should I use? none of them fires the onopen method
                clientSocket.Send(System.Text.Encoding.UTF8.GetBytes(response));
            }





            // keep track of the new guy
            var clientConnection = new WebSocketConnection(clientSocket);
            Connections.Add(clientConnection);
            // clientConnection.Disconnected += new WebSocketDisconnectedEventHandler(ClientDisconnected);
            Console.WriteLine("New user: " + ipLocal);
            // invoke the connection event
            if (ClientConnected != null)
                ClientConnected(clientConnection, EventArgs.Empty);

            if (LogLevel != ServerLogLevel.Nothing)
                clientConnection.DataReceived += new DataReceivedEventHandler(DataReceivedFromClient);



            // listen for more clients
            ListenForClients();

        }

        void ClientDisconnected(WebSocketConnection sender, EventArgs e)
        {
            Connections.Remove(sender);
            LogLine(DateTime.Now + "> " + sender.ConnectionSocket.LocalEndPoint + " disconnected", ServerLogLevel.Subtle);
        }

        void DataReceivedFromClient(WebSocketConnection sender, DataReceivedEventArgs e)
        {
            Log(DateTime.Now + "> data from " + sender.ConnectionSocket.LocalEndPoint, ServerLogLevel.Subtle);
            Log(": " + e.Data + "\n" + e.Size + " bytes", ServerLogLevel.Verbose);
            LogLine("", ServerLogLevel.Subtle);
        }


        /// <summary>
        /// send a string to all the clients (you spammer!)
        /// </summary>
        /// <param name="data">the string to send</param>
        public void SendToAll(string data)
        {
            Connections.ForEach(a => a.Send(data));
        }

        /// <summary>
        /// send a string to all the clients except one
        /// </summary>
        /// <param name="data">the string to send</param>
        /// <param name="indifferent">the client that doesn't care</param>
        public void SendToAllExceptOne(string data, WebSocketConnection indifferent)
        {
            foreach (var client in Connections)
            {
                if (client != indifferent)
                    client.Send(data);
            }
        }

        /// <summary>
        /// Takes care of the initial handshaking between the the client and the server
        /// </summary>


        private void Log(string str, ServerLogLevel level)
        {
            if (Logger != null && (int)LogLevel >= (int)level)
            {
                Logger.Write(str);
            }
        }

        private void LogLine(string str, ServerLogLevel level)
        {
            Log(str + "\r\n", level);
        }

        private static string AcceptKey(ref string key)
        {
            string longKey = key + guid;
            byte[] hashBytes = ComputeHash(longKey);
            return Convert.ToBase64String(hashBytes);
        }
        static SHA1 sha1 = SHA1CryptoServiceProvider.Create();
        private static byte[] ComputeHash(string str)
        {
            return sha1.ComputeHash(System.Text.Encoding.ASCII.GetBytes(str));
        }



        private void ShakeHands(Socket conn)
        {
            using (var stream = new NetworkStream(conn))
            using (var reader = new StreamReader(stream))
            using (var writer = new StreamWriter(stream))
            {
                //read handshake from client (no need to actually read it, we know its there):
                LogLine("Reading client handshake:", ServerLogLevel.Verbose);
                string r = null;
                while (r != "")
                {
                    r = reader.ReadLine();
                    LogLine(r, ServerLogLevel.Verbose);
                }

                // send handshake to the client
                writer.WriteLine("HTTP/1.1 101 Web Socket Protocol Handshake");
                writer.WriteLine("Upgrade: WebSocket");
                writer.WriteLine("Connection: Upgrade");
                writer.WriteLine("WebSocket-Origin: " + webSocketOrigin);
                writer.WriteLine("WebSocket-Location: " + webSocketLocation);
                writer.WriteLine("");
            }


            // tell the nerds whats going on
            LogLine("Sending handshake:", ServerLogLevel.Verbose);
            LogLine("HTTP/1.1 101 Web Socket Protocol Handshake", ServerLogLevel.Verbose);
            LogLine("Upgrade: WebSocket", ServerLogLevel.Verbose);
            LogLine("Connection: Upgrade", ServerLogLevel.Verbose);
            LogLine("WebSocket-Origin: " + webSocketOrigin, ServerLogLevel.Verbose);
            LogLine("WebSocket-Location: " + webSocketLocation, ServerLogLevel.Verbose);
            LogLine("", ServerLogLevel.Verbose);

            LogLine("Started listening to client", ServerLogLevel.Verbose);
            //conn.Listen();
        }


    }
}

连接问题已解决,下一步将是向客户端发送数据。


你解决了发送数据的问题吗?我下载了相同的示例,遇到了完全相同的问题 >_< 我自己解决了连接问题,但是现在又出现了发送数据的问题。唉,这个烂代码谁会把它称为“Websockets服务器示例”呢? - Kosmo零

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接