WebSocket的服务器:在网络上插座的OnOpen功能不会被调用 [英] Websocket server: onopen function on the web socket is never called

查看:225
本文介绍了WebSocket的服务器:在网络上插座的OnOpen功能不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个C#网络套接字服务器,但它给我一些麻烦。
我运行一个Web服务器(ASP.NET)使用JavaScript和Web套接字服务器承载的网页被实现为一个C#控制台应用程序。

I'm trying to implement a C# web socket server, but its giving me a few troubles. I'm running a webserver(ASP.NET) to host the page with the javascript and the web socket server is implemented as a C# console application.

我能够检测来自客户机的连接尝试(铬运行的JavaScript)并且还从客户端检索握手。但是客户似乎并不接受我回送握手(在网络上插座的OnOpen 函数永远不会被调用)。

I'm able to detect the connection attempt from the client (chrome running the javascript) and also to retrieve the handshake from the client. But the client doesn't seem to accept the handshake I'm sending back (the onopen function on the web socket is never called).

我一直在阅读的网络套接字协议,我不能看到什么我做错了。
继承人位服务器code的:

I've been reading the The Web Socket protocol and I can't see what I'm doing wrong. Heres a bit of the server code:

Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8181);
listener.Bind(ep);
listener.Listen(100);
Console.WriteLine("Wainting for connection...");
Socket socketForClient = listener.Accept();
if (socketForClient.Connected)
{
    Console.WriteLine("Client connected");
    NetworkStream networkStream = new NetworkStream(socketForClient);
    System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream);
    System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream);

    //read handshake from client:
    Console.WriteLine("HANDSHAKING...");
    char[] shake = new char[255];
    streamReader.Read(shake, 0, 255);

    string handshake =
       "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" +
       "Upgrade: WebSocket\r\n" +
       "Connection: Upgrade\r\n" +
       "WebSocket-Origin: http://localhost:8080\r\n" +
       "WebSocket-Location: ws://localhost:8181\r\n" +
       "\r\n";

    streamWriter.Write(handshake);
    streamWriter.Flush();

我在端口8181上运行到Web服务器上的端口8080和网络套接字服务器,都在我的本地。

I'm running to web server on port 8080 and the web socket server on port 8181, both on my localhost.

我试着发送不同的编码(ASCII,字节,十六进制)握手,但这似乎并没有发挥作用。该连接不会完全建立。
JavaScript的看起来是这样的:

I've tried sending the handshake in different encodings (ASCII, bytes and Hex) but this doesn't seem to make a difference. The connection is never fully established. The javascript looks like this:

var ws;
var host = 'ws://localhost:8181';
debug("Connecting to " + host + " ...");
try {
 ws = new WebSocket(host);
} catch (err) {
 debug(err, 'error');
}
ws.onopen = function () {
 debug("connected...", 'success');
};
ws.onclose = function () {
 debug("Socket closed!", 'error');
};
ws.onmessage = function (evt) {
 debug('response: ' + evt, 'response');
};

我猜测,错误出在C#服务器铬发送的信息,因为它应该,但作为一个透露,的OnOpen 函数永远不会被调用。

在短暂的我的问题:
有任何你曾经做到了这一点 - 如果是,你是怎么做到的呢?
和事业:你看到在code任何明显的错误(希望那不是过分的要求)

So my question in short: Has any of you ever accomplished this - and if yes, how did you do it? And of cause: Do you see any apparent errors in the code (hope thats not to much to ask)

推荐答案

也许这是一个编码的问题。下面是我写了一个工作的C#服务器:

Probably it's an encoding issue. Here's a working C# server I wrote:

class Program
{
    static void Main(string[] args)
    {
        var listener = new TcpListener(IPAddress.Loopback, 8181);
        listener.Start();
        using (var client = listener.AcceptTcpClient())
        using (var stream = client.GetStream())
        using (var reader = new StreamReader(stream))
        using (var writer = new StreamWriter(stream))
        {
            writer.WriteLine("HTTP/1.1 101 Web Socket Protocol Handshake");
            writer.WriteLine("Upgrade: WebSocket");
            writer.WriteLine("Connection: Upgrade");
            writer.WriteLine("WebSocket-Origin: http://localhost:8080");
            writer.WriteLine("WebSocket-Location: ws://localhost:8181/websession");
            writer.WriteLine("");
        }
        listener.Stop();
    }
}

和相应的客户端托管本地主机:8080

<!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:8181/websession');
      socket.onopen = function() {
        alert('handshake successfully established. May send data now...');
      };
      socket.onclose = function() {
        alert('connection closed');
      };
    </script>
  </head>
  <body>
  </body>
</html>

这个例子只建立了握手。您将需要调整服务器,以一次握手已经成立,以继续接收数据。

This example only establishes the handshake. You will need to tweak the server in order to continue accepting data once the handshake has been established.

这篇关于WebSocket的服务器:在网络上插座的OnOpen功能不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆