无法通过 websocket 从服务器向客户端发送数据

Posted

技术标签:

【中文标题】无法通过 websocket 从服务器向客户端发送数据【英文标题】:Could not send data from Server to client via websocket 【发布时间】:2013-03-14 08:14:45 【问题描述】:

我想我需要帮忙。我的理解是,一旦我向客户端发送数据,onmessage() 就会被触发。

我用 C# 创建了我的 websocket 服务器,用 html5 创建了我的客户端。我可以成功建立 websocket 连接,但无法向客户端发送数据。或者,我已经在发送,但没有在阅读。好吧,它只是不会激发我的onmessage()。请帮忙。

C#(.net 4.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();
        


    

HTML5:

<!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://127.0.0.1:8080");  
            socket.onopen = function(evt)  
                alert("Socket has been opened!");  

              
            socket.onmessage = function(evt)       
                alert("On message fired up!");
            ;
        );
        </script>  
    </head>
</HTML>

这就是我在响应标头中得到的:

这是我的发送方法:

public void Send(string str)
        
            if (ConnectionSocket.Connected)
            
                try
                

                    // start with a 0x00
                    ConnectionSocket.Send(new byte[]  (byte)WrapperBytes.Start , 1, 0);
                    // send the string
                    ConnectionSocket.Send(Encoding.UTF8.GetBytes(str));
                    /*
                    writer.Write(str);
                    writer.Flush();*/

                    // end with a 0xFF
                    ConnectionSocket.Send(new byte[]  (byte)WrapperBytes.End , 1, 0);
                
                catch
                
                    if (Disconnected != null)

                        Disconnected(this, EventArgs.Empty);
                
            
        

【问题讨论】:

Websocket 消息不作为纯文本发送。每条消息都必须按照 RFC6455 的data framing section 中的描述进行构建。您的WebSocketConnection 类的Send 方法是否应用此框架? 嗨..我更新了我的问题。我提供了发送方法的代码 谢谢,看来我的第一条评论是正确的。我已经发布了一个更详细的答案。 .net framework 4.0 是否支持这种数据框架? (替换之前的错误评论)不,.net 4.0 不支持 websockets。为此,您需要迁移到 .net 4.5。有关详细信息,请参阅 System.Net.WebSockets 命名空间的 MSDN 文档。 【参考方案1】:

websocket 协议在早期草案之间经历了一些不兼容的变化。看起来您的代码分为支持最新版本和早期版本。握手基于RFC6455(或其早期的Hybi草案)出现;消息发送似乎基于早期不兼容的 Hixie-76 变体。

您的服务器可以支持多种协议变体。几乎所有浏览器(Windows 版 Safari 可能是一个例外)现在都支持更新的变体,所以我认为这是您要优先考虑的版本。

要支持最新的协议变体,您需要更改 WebSocketConnection.Send 以实现 RFC6455 的 data framing section 中所述的框架。

WebSocketConnection 中接收消息的代码可能需要进行类似更改。

【讨论】:

以上是关于无法通过 websocket 从服务器向客户端发送数据的主要内容,如果未能解决你的问题,请参考以下文章

无法从 React 客户端向 Node.js websocket 服务器发送消息

通过 Websockets 从 Python Flask 服务器连续向客户端发送数据

无法解码来自 Websocket 的消息

websocket——通信梳理(短轮询、长轮询、socket、websocket、socket.io)

如何使用 STOMP 从 Spring WebSocket 服务器向 WebSocket 客户端发送消息?

通过 websocket 继续向客户端发送数据