96

我不明白为什么我不能使下面的代码工作。我想用 JavaScript 连接到我的服务器控制台应用程序。然后将数据发送到服务器。

这是服务器代码:

    static void Main(string[] args)
    {            
        TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 9998);
        server.Start();
        var client = server.AcceptTcpClient();
        var stream = client.GetStream();

        while (true)
        {
            var buffer = new byte[1024]; 
            // wait for data to be received
            var bytesRead = stream.Read(buffer, 0, buffer.Length);                
            var r = System.Text.Encoding.UTF8.GetString(buffer);
            // write received data to the console
            Console.WriteLine(r.Substring(0, bytesRead));
        }
    }

这是JavaScript:

        var ws = new WebSocket("ws://localhost:9998/service");
        ws.onopen = function () {
            ws.send("Hello World"); // I WANT TO SEND THIS MESSAGE TO THE SERVER!!!!!!!!
        };

        ws.onmessage = function (evt) {
            var received_msg = evt.data;
            alert("Message is received...");
        };
        ws.onclose = function () {
            // websocket is closed.
            alert("Connection is closed...");
        };

当我运行该代码时,会发生以下情况:

请注意,当我运行 JavaScript 时,服务器接受并成功建立连接。JavaScript 无法发送数据。每当我放置发送方法时,即使建立了连接,它也不会发送。我怎样才能使这项工作?

4

5 回答 5

78

WebSockets 是依赖于 TCP 流连接的协议。虽然 WebSockets 是基于消息的协议。

如果您想实现自己的协议,那么我建议使用最新且稳定的规范(适用于 18/04/12)RFC 6455。本规范包含有关握手和成帧的所有必要信息。以及大部分关于浏览器端和服务器端行为场景的描述。强烈建议在实施代码期间遵循有关服务器端的建议。

简而言之,我将描述像这样使用 WebSockets:

  1. 创建服务器 Socket (System.Net.Sockets) 将其绑定到特定端口,并通过异步接受连接继续监听。像这样的东西:
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
serverSocket.Bind(new IPEndPoint(IPAddress.Any, 8080));
serverSocket.Listen(128);
serverSocket.BeginAccept(null, 0, OnAccept, null);
  1. 您应该具有将实现握手的接受函数“OnAccept”。将来,如果系统打算每秒处理大量连接,它必须在另一个线程中。
私人无效 OnAccept(IAsyncResult 结果){
    尝试 {
        套接字客户端 = null;
        if (serverSocket != null && serverSocket.IsBound) {
            客户端 = serverSocket.EndAccept(结果);
        }
        如果(客户端!= null){
            /* 握手和管理 ClientSocket */
        }
    } 捕捉(SocketException 异常) {

    } 最后 {
        if (serverSocket != null && serverSocket.IsBound) {
            serverSocket.BeginAccept(null, 0, OnAccept, null);
        }
    }
}
  1. 建立连接后,您必须进行握手。根据规范1.3 打开握手,连接建立后,您将收到带有一些信息的基本 HTTP 请求。例子:
获取/聊天 HTTP/1.1
主机:server.example.com
升级:websocket
连接:升级
Sec-WebSocket-Key:dGhlIHNhbXBsZSBub25jZQ==
来源:http://example.com
Sec-WebSocket-Protocol:聊天、超级聊天
Sec-WebSocket-版本:13

此示例基于协议 13 的版本。请记住,旧版本有一些差异,但大多数最新版本是交叉兼容的。不同的浏览器可能会向您发送一些额外的数据。例如浏览器和操作系统详细信息、缓存等。

根据提供的握手细节,您必须生成答案行,它们大多相同,但将包含 Accept-Key,即基于提供的 Sec-WebSocket-Key。在规范 1.3 中清楚地描述了如何生成响应密钥。这是我一直用于 V13 的函数:

静态私有字符串 guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
私人字符串AcceptKey(参考字符串键){
        字符串 longKey = 键 + guid;
        SHA1 sha1 = SHA1CryptoServiceProvider.Create();
        byte[] hashBytes = sha1.ComputeHash(System.Text.Encoding.ASCII.GetBytes(longKey));
        返回 Convert.ToBase64String(hashBytes);
}

握手答案如下所示:

HTTP/1.1 101 交换协议
升级:websocket
连接:升级
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

但是接受密钥必须是基于客户端提供的密钥和我之前提供的方法 AcceptKey 生成的密钥。同样,确保在接受键的最后一个字符之后添加两个新行“\r\n\r\n”。

  1. 握手应答从服务器发送后,客户端应该触发“ onopen ”功能,这意味着你可以发送消息。

  2. 消息不是以原始格式发送的,但它们具有Data Framing。并且从客户端到服务器也根据消息头中提供的 4 个字节实现数据屏蔽。尽管从服务器到客户端,您不需要对数据应用屏蔽。阅读规范中的第5 节。数据框架。这是我自己实现的复制粘贴。它不是即用型代码,必须进行修改,我发布它只是为了提供一个想法和使用 WebSocket 框架读/写的整体逻辑。转到此链接

  3. 实现框架后,请确保您使用套接字以正确的方式接收数据。例如,为了防止某些消息合并为一个,因为 TCP 仍然是基于流的协议。这意味着您必须只读取特定数量的字节。消息的长度始终基于标头,并在标头中提供数据长度详细信息。因此,当您从 Socket 接收数据时,首先接收 2 个字节,根据帧规范从标头获取详细信息,然后如果掩码提供另外 4 个字节,然后根据数据长度可能是 1、4 或 8 个字节的长度。在数据它自己之后。阅读后,应用去掩码,您的消息数据就可以使用了。

  4. 您可能想要使用一些数据协议,我建议使用 JSON,因为流量经济且易于在 JavaScript 客户端使用。对于服务器端,您可能需要检查一些解析器。有很多,谷歌真的很有帮助。

实现自己的 WebSockets 协议肯定有一些好处和丰富的经验,以及对协议本身的控制。但是你必须花一些时间去做,并确保实现是高度可靠的。

同时,您可能会看到谷歌(再次)有足够的现成可用的解决方案。

于 2012-04-18T09:46:16.603 回答
13

(代表 OP 发布答案)

我现在可以发送数据了。感谢您的回答和@Maksims Mihejevs 的代码,这是我的新版本程序。

服务器

using System;
using System.Net.Sockets;
using System.Net;
using System.Security.Cryptography;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static Socket serverSocket = new Socket(AddressFamily.InterNetwork, 
        SocketType.Stream, ProtocolType.IP);
        static private string guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";

        static void Main(string[] args)
        {            
            serverSocket.Bind(new IPEndPoint(IPAddress.Any, 8080));
            serverSocket.Listen(128);
            serverSocket.BeginAccept(null, 0, OnAccept, null);            
            Console.Read();
        }

        private static void OnAccept(IAsyncResult result)
        {
            byte[] buffer = new byte[1024];
            try
            {
                Socket client = null;
                string headerResponse = "";
                if (serverSocket != null && serverSocket.IsBound)
                {
                    client = serverSocket.EndAccept(result);
                    var i = client.Receive(buffer);
                    headerResponse = (System.Text.Encoding.UTF8.GetString(buffer)).Substring(0,i);
                    // write received data to the console
                    Console.WriteLine(headerResponse);

                }
                if (client != null)
                {
                    /* Handshaking and managing ClientSocket */

                    var key = headerResponse.Replace("ey:", "`")
                              .Split('`')[1]                     // dGhlIHNhbXBsZSBub25jZQ== \r\n .......
                              .Replace("\r", "").Split('\n')[0]  // dGhlIHNhbXBsZSBub25jZQ==
                              .Trim();

                    // key should now equal dGhlIHNhbXBsZSBub25jZQ==
                    var test1 = AcceptKey(ref key);

                    var newLine = "\r\n";

                    var response = "HTTP/1.1 101 Switching Protocols" + newLine
                         + "Upgrade: websocket" + newLine
                         + "Connection: Upgrade" + newLine
                         + "Sec-WebSocket-Accept: " + test1 + newLine + newLine
                         //+ "Sec-WebSocket-Protocol: chat, superchat" + newLine
                         //+ "Sec-WebSocket-Version: 13" + newLine
                         ;

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

                    var i = client.Receive(buffer); // wait for client to send a message

                    // once the message is received decode it in different formats
                    Console.WriteLine(Convert.ToBase64String(buffer).Substring(0, i));                    

                    Console.WriteLine("\n\nPress enter to send data to client");
                    Console.Read();

                    var subA = SubArray<byte>(buffer, 0, i);
                    client.Send(subA);
                    Thread.Sleep(10000);//wait for message to be send


                }
            }
            catch (SocketException exception)
            {
                throw exception;
            }
            finally
            {
                if (serverSocket != null && serverSocket.IsBound)
                {
                    serverSocket.BeginAccept(null, 0, OnAccept, null);
                }
            }
        }

        public static T[] SubArray<T>(T[] data, int index, int length)
        {
            T[] result = new T[length];
            Array.Copy(data, index, result, 0, length);
            return result;
        }

        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));
        }
    }
}

JavaScript:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript">
        function connect() {
            var ws = new WebSocket("ws://localhost:8080/service");
            ws.onopen = function () {
                alert("About to send data");
                ws.send("Hello World"); // I WANT TO SEND THIS MESSAGE TO THE SERVER!!!!!!!!
                alert("Message sent!");
            };

            ws.onmessage = function (evt) {
                alert("About to receive data");
                var received_msg = evt.data;
                alert("Message received = "+received_msg);
            };
            ws.onclose = function () {
                // websocket is closed.
                alert("Connection is closed...");
            };
        };


    </script>
</head>
<body style="font-size:xx-large" >
    <div>
    <a href="#" onclick="connect()">Click here to start</a></div>
</body>
</html>

当我运行该代码时,我能够从客户端和服务器发送和接收数据。唯一的问题是消息在到达服务器时是加密的。以下是程序如何运行的步骤:

在此处输入图像描述

请注意来自客户端的消息是如何加密的。

于 2017-07-27T10:39:54.177 回答
9

我在任何地方都找不到一个简单的工作示例(截至 1 月 19 日),所以这是一个更新版本。我有 chrome 版本 71.0.3578.98。

C# Websocket 服务器:

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;

namespace WebSocketServer
{
    class Program
    {
    static Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
    static private string guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";

    static void Main(string[] args)
    {
        serverSocket.Bind(new IPEndPoint(IPAddress.Any, 8080));
        serverSocket.Listen(1); //just one socket
        serverSocket.BeginAccept(null, 0, OnAccept, null);
        Console.Read();
    }

    private static void OnAccept(IAsyncResult result)
    {
        byte[] buffer = new byte[1024];
        try
        {
            Socket client = null;
            string headerResponse = "";
            if (serverSocket != null && serverSocket.IsBound)
            {
                client = serverSocket.EndAccept(result);
                var i = client.Receive(buffer);
                headerResponse = (System.Text.Encoding.UTF8.GetString(buffer)).Substring(0, i);
                // write received data to the console
                Console.WriteLine(headerResponse);
                Console.WriteLine("=====================");
            }
            if (client != null)
            {
                /* Handshaking and managing ClientSocket */
                var key = headerResponse.Replace("ey:", "`")
                          .Split('`')[1]                     // dGhlIHNhbXBsZSBub25jZQ== \r\n .......
                          .Replace("\r", "").Split('\n')[0]  // dGhlIHNhbXBsZSBub25jZQ==
                          .Trim();

                // key should now equal dGhlIHNhbXBsZSBub25jZQ==
                var test1 = AcceptKey(ref key);

                var newLine = "\r\n";

                var response = "HTTP/1.1 101 Switching Protocols" + newLine
                     + "Upgrade: websocket" + newLine
                     + "Connection: Upgrade" + newLine
                     + "Sec-WebSocket-Accept: " + test1 + newLine + newLine
                     //+ "Sec-WebSocket-Protocol: chat, superchat" + newLine
                     //+ "Sec-WebSocket-Version: 13" + newLine
                     ;

                client.Send(System.Text.Encoding.UTF8.GetBytes(response));
                var i = client.Receive(buffer); // wait for client to send a message
                string browserSent = GetDecodedData(buffer, i);
                Console.WriteLine("BrowserSent: " + browserSent);

                Console.WriteLine("=====================");
                //now send message to client
                client.Send(GetFrameFromString("This is message from server to client."));
                System.Threading.Thread.Sleep(10000);//wait for message to be sent
            }
        }
        catch (SocketException exception)
        {
            throw exception;
        }
        finally
        {
            if (serverSocket != null && serverSocket.IsBound)
            {
                serverSocket.BeginAccept(null, 0, OnAccept, null);
            }
        }
    }

    public static T[] SubArray<T>(T[] data, int index, int length)
    {
        T[] result = new T[length];
        Array.Copy(data, index, result, 0, length);
        return result;
    }

    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));
    }

    //Needed to decode frame
    public static string GetDecodedData(byte[] buffer, int length)
    {
        byte b = buffer[1];
        int dataLength = 0;
        int totalLength = 0;
        int keyIndex = 0;

        if (b - 128 <= 125)
        {
            dataLength = b - 128;
            keyIndex = 2;
            totalLength = dataLength + 6;
        }

        if (b - 128 == 126)
        {
            dataLength = BitConverter.ToInt16(new byte[] { buffer[3], buffer[2] }, 0);
            keyIndex = 4;
            totalLength = dataLength + 8;
        }

        if (b - 128 == 127)
        {
            dataLength = (int)BitConverter.ToInt64(new byte[] { buffer[9], buffer[8], buffer[7], buffer[6], buffer[5], buffer[4], buffer[3], buffer[2] }, 0);
            keyIndex = 10;
            totalLength = dataLength + 14;
        }

        if (totalLength > length)
            throw new Exception("The buffer length is small than the data length");

        byte[] key = new byte[] { buffer[keyIndex], buffer[keyIndex + 1], buffer[keyIndex + 2], buffer[keyIndex + 3] };

        int dataIndex = keyIndex + 4;
        int count = 0;
        for (int i = dataIndex; i < totalLength; i++)
        {
            buffer[i] = (byte)(buffer[i] ^ key[count % 4]);
            count++;
        }

        return Encoding.ASCII.GetString(buffer, dataIndex, dataLength);
    }

    //function to create  frames to send to client 
    /// <summary>
    /// Enum for opcode types
    /// </summary>
    public enum EOpcodeType
    {
        /* Denotes a continuation code */
        Fragment = 0,

        /* Denotes a text code */
        Text = 1,

        /* Denotes a binary code */
        Binary = 2,

        /* Denotes a closed connection */
        ClosedConnection = 8,

        /* Denotes a ping*/
        Ping = 9,

        /* Denotes a pong */
        Pong = 10
    }

    /// <summary>Gets an encoded websocket frame to send to a client from a string</summary>
    /// <param name="Message">The message to encode into the frame</param>
    /// <param name="Opcode">The opcode of the frame</param>
    /// <returns>Byte array in form of a websocket frame</returns>
    public static byte[] GetFrameFromString(string Message, EOpcodeType Opcode = EOpcodeType.Text)
    {
        byte[] response;
        byte[] bytesRaw = Encoding.Default.GetBytes(Message);
        byte[] frame = new byte[10];

        long indexStartRawData = -1;
        long length = (long)bytesRaw.Length;

        frame[0] = (byte)(128 + (int)Opcode);
        if (length <= 125)
        {
            frame[1] = (byte)length;
            indexStartRawData = 2;
        }
        else if (length >= 126 && length <= 65535)
        {
            frame[1] = (byte)126;
            frame[2] = (byte)((length >> 8) & 255);
            frame[3] = (byte)(length & 255);
            indexStartRawData = 4;
        }
        else
        {
            frame[1] = (byte)127;
            frame[2] = (byte)((length >> 56) & 255);
            frame[3] = (byte)((length >> 48) & 255);
            frame[4] = (byte)((length >> 40) & 255);
            frame[5] = (byte)((length >> 32) & 255);
            frame[6] = (byte)((length >> 24) & 255);
            frame[7] = (byte)((length >> 16) & 255);
            frame[8] = (byte)((length >> 8) & 255);
            frame[9] = (byte)(length & 255);

            indexStartRawData = 10;
        }

        response = new byte[indexStartRawData + length];

        long i, reponseIdx = 0;

        //Add the frame bytes to the reponse
        for (i = 0; i < indexStartRawData; i++)
        {
            response[reponseIdx] = frame[i];
            reponseIdx++;
        }

        //Add the data bytes to the response
        for (i = 0; i < length; i++)
        {
            response[reponseIdx] = bytesRaw[i];
            reponseIdx++;
        }

        return response;
    }
}
}

客户端 html 和 javascript:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript">
        var socket = new WebSocket('ws://localhost:8080/websession');
        socket.onopen = function() {
           // alert('handshake successfully established. May send data now...');
           socket.send("Hi there from browser.");
        };
        socket.onmessage = function (evt) {
                //alert("About to receive data");
                var received_msg = evt.data;
                alert("Message received = "+received_msg);
            };
        socket.onclose = function() {
            alert('connection closed');
        };
    </script>
</head>
<body>
</body>
</html>

于 2019-01-15T14:51:25.330 回答
6

WebSockets 是使用涉及客户端和服务器之间握手的协议实现的。我不认为它们的工作方式与普通插座非常相似。阅读协议,并让您的应用程序谈论它。或者,使用现有的 WebSocket 库或具有WebSocket API的 .Net4.5beta 。

于 2012-04-18T00:05:10.637 回答
3

问题

由于您使用的是 WebSocket,因此花费者是正确的。从 WebSocket 接收到初始数据后,您需要从 C# 服务器发送握手消息,然后才能传输任何进一步的信息。

HTTP/1.1 101 Web Socket Protocol Handshake
Upgrade: websocket
Connection: Upgrade
WebSocket-Origin: example
WebSocket-Location: something.here
WebSocket-Protocol: 13

类似的东西。

您可以对 WebSocket 在 w3 或 google 上的工作方式进行更多研究。

链接和资源

这是一个协议规范:https ://datatracker.ietf.org/doc/html/draft-hixie-thewebsocketprotocol-76#section-1.3

工作示例列表:

于 2012-04-18T00:14:05.540 回答