简体   繁体   English

C#Websocket服务器未从HTML5客户端接收消息

[英]C# Websocket server not receiving messages from HTML5 client

I'm attempting to create a bare bones websocket chat room server. 我正在尝试创建一个简单的websocket聊天室服务器。 My client is able to connect to the server, but it is unable to send messages to the server, despite the server being in a listening state. 我的客户端能够连接到服务器,但是即使服务器处于侦听状态,也无法将消息发送到服务器。

When the client connects, a bunch of what looks like header information gets written to the console. 当客户端连接时,一堆看起来像标题信息的东西将被写入控制台。 But when WebSocket.send() gets executed in Javascript, nothing occurs server side. 但是,当用Javascript执行WebSocket.send()时,服务器端不会发生任何事情。

HTML: HTML:

<button id="closeSocket">disconnect</button><br />
<input id = "inputField" /><button id="sendMessage">send</button>
<div id = "output"></div>

<script type = 'text/javascript' src = 'websockets.js'></script>

Javascript: Javascript:

websocket = new WebSocket("ws://127.0.0.1:80");

document.getElementById("closeSocket").onclick = closeSocket;
document.getElementById("sendMessage").onclick = sendMessage;

websocket.onopen = function(){
  output("connected");
}

function sendMessage(){
    output("sent: " + document.getElementById('inputField').value);
  websocket.send(document.getElementById('inputField').value);
}

websocket.onmessage = function(e){
  output("got response: " + e.data);
}

function closeSocket(){
    websocket.close();
}

websocket.onclose = function(){
    output("disconnected");
}

function output(t){
    document.getElementById("output").innerHTML += t + "<br />";
}

C# server: C#服务器:

using System;
using System.Net;
using System.Net.Sockets;

namespace WebSocketsTutorial {
    class Program {
        static void Main(string[] args) {
            TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 80);
            TcpClient client = default(TcpClient);

            server.Start();
            Console.WriteLine("server started");

            while (true) {
                client = server.AcceptTcpClient();

                byte[] receivedBuffer = new byte[100];
                NetworkStream stream = client.GetStream();

                stream.Read(receivedBuffer, 0, receivedBuffer.Length);

                foreach (byte b in receivedBuffer) {
                    Console.Write(Convert.ToChar(b).ToString());
                }
            }
        }
    }
}

This is what is output on the console when the client connects: 这是客户端连接时控制台上输出的内容:

在此处输入图片说明

What I'm mainly looking to do is to allow an arbitrary number of connections and ultimately have the server echo a user's submission to all connected clients. 我主要要做的是允许任意数量的连接,并最终让服务器向所有连接的客户端回显用户的提交。

First of all, a WebSocket is not just a regular socket. 首先,WebSocket不仅仅是普通的套接字。 It defines a connection handshake using HTTP and its own framing protocol that you need to use. 它使用HTTP和您需要使用的自己的框架协议定义连接握手。

https://en.wikipedia.org/wiki/WebSocket https://zh.wikipedia.org/wiki/WebSocket

https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers https://developer.mozilla.org/zh-CN/docs/Web/API/WebSockets_API/Writing_WebSocket_servers

https://hpbn.co/websocket/ https://hpbn.co/websocket/

Second, you are reading just the first 100 bytes of the request. 其次,您仅读取请求的前100个字节。 You should read until the Read operation returns 0. 您应该读取,直到Read操作返回0。

There is a myriad of components you can use to create a WebSocket server, including the default one: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/websockets 您可以使用许多组件来创建WebSocket服务器,包括默认的组件: https : //docs.microsoft.com/zh-cn/aspnet/core/fundamentals/websockets

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM