c# - POCO 1.5.1 Websocket 客户端无法连接到 c# websocket 服务器

标签 c# c++ websocket poco eventmachine

问题:

websocket 客户端(POCO 1.5.1,c++)不会连接到 websocket c# 服务器(带有 Fleck 库的命令行应用程序)。达到超时并抛出异常:

Cannot upgrade to WebSocket connection: OK
Exception Code: 1

Poco WebException Documentation
WS_ERR_NO_HANDSHAKE = 1 : 
No Connection: Upgrade or Upgrade: websocket header in handshake request.

事实 1:此 websocket 客户端将连接到 Ruby Event Machine websocket 服务器。

事实 2:javascript 客户端将连接到 websocket c# 服务器

事实 3:相同的 javascript 客户端 也将连接到 websocket ruby​​ 服务器

事实 4:websocket 客户端 不会连接到附近的 Alchemy Websocket 服务器https://github.com/Olivine-Labs/Alchemy-Websockets

使用 Wireshark 更新 POCO正在使用 GET/HTTP/1.0\r\n

Javascript 版本: GET/HTTP/1.1\r\n


所有源代码

c++ 客户端代码

#include "Game.h"

#include <irrlicht.h>
#include "driverChoice.h"
#include <iostream>
#include <assert.h>

#include "Poco/Net/WebSocket.h"
#include "Poco/Net/HTTPClientSession.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/Net/ServerSocket.h"
#include "Poco/Net/NetException.h"
#include "Poco/Exception.h"

using Poco::Net::HTTPClientSession;
using Poco::Net::HTTPRequest;
using Poco::Net::HTTPResponse;
using Poco::Net::HTTPServerRequest;
using Poco::Net::HTTPServerResponse;
using Poco::Net::WebSocket;
using Poco::Net::WebSocketException;
using Poco::Exception;

// VS2010
// POCO 1.5.1
// Irrlicht 3D engine
// Windows 7 Enterprise edition

Game::Game(void)
{
}

Game::~Game(void)
{
}

//...

void Game::TestWebSocketClient()
{
  char buffer[1024];
  int flags;
  int n;
  std::string payload;

  try
  {
      HTTPClientSession cs("localhost", 8080);
      HTTPRequest request(HTTPRequest::HTTP_GET, "/ws");
      HTTPResponse response;
      std::string cmd;

      WebSocket * ws = new WebSocket(cs, request, response); // Causes the timeout

      payload = "SGClient: Hello World!";
      ws->sendFrame(payload.data(), payload.size(), WebSocket::FRAME_TEXT);
      n = ws->receiveFrame(buffer, sizeof(buffer), flags);

      while( cmd != "exit")
      {
        cmd = "";
        std::cin >> cmd;
        ws->sendFrame(cmd.data(), cmd.size(), WebSocket::FRAME_TEXT);
        n = ws->receiveFrame(buffer, sizeof(buffer), flags);
        if( n > 0 )
        {
            std::cout << buffer << std::endl;
        }
      }

      ws->shutdown();
  }
  catch (Exception ex)
  {
      return;
  }

c# 中的服务器代码

// vs2010
// fleck library
// Windows 7 enterprise edition


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

namespace TestWebsocket
{
    class Program
    {
        static void Main()
        {
            FleckLog.Level = LogLevel.Debug;
            var allSockets = new List<IWebSocketConnection>();
            var server = new WebSocketServer("ws://localhost:8080");
            server.Start(socket =>
            {
                socket.OnOpen = () =>
                {
                    Console.WriteLine("Open!");
                    allSockets.Add(socket);
                };
                socket.OnClose = () =>
                {
                    Console.WriteLine("Close!");
                    allSockets.Remove(socket);
                };
                socket.OnMessage = message =>
                {
                    Console.WriteLine(message);
                    allSockets.ToList().ForEach(s => s.Send("Echo: " + message));
                };
            });

            var input = Console.ReadLine();
            while (input != "exit")
            {
                foreach (var socket in allSockets.ToList())
                {
                    socket.Send(input);
                }
                input = Console.ReadLine();
            }

        }
    }
}

备用 HTML/Javascript 客户端代码,在 Chrome 版本 31.0.1650.57 m 中运行

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <title>websocket client</title>
    <script type="text/javascript">
        var start = function () {
            var inc = document.getElementById('incomming');
            var wsImpl = window.WebSocket || window.MozWebSocket;
            var form = document.getElementById('sendForm');
            var input = document.getElementById('sendText');

            inc.innerHTML += "connecting to server ..<br/>";

            // create a new websocket and connect
            window.ws = new wsImpl('ws://localhost:8080/');

            // when data is comming from the server, this metod is called
            ws.onmessage = function (evt) {
                inc.innerHTML += evt.data + '<br/>';
            };

            // when the connection is established, this method is called
            ws.onopen = function () {
                inc.innerHTML += '.. connection open<br/>';
            };

            // when the connection is closed, this method is called
            ws.onclose = function () {
                inc.innerHTML += '.. connection closed<br/>';
            }

            form.addEventListener('submit', function(e){
                e.preventDefault();
                var val = input.value;
                ws.send(val);
                input.value = "";
            });

        }
        window.onload = start;
    </script>
</head>
<body>
    <form id="sendForm">
        <input id="sendText" placeholder="Text to send" />
    </form>
    <pre id="incomming"></pre>
</body>
</html>

使用在命令行界面上运行的 eventmachine 的备用 ruby​​ 服务器代码

// Ruby 1.9
// gem install em-websocket required.
require 'em-websocket'

EventMachine::WebSocket.start(:host => "localhost", :port => 8080) do |ws|
  ws.onopen    { ws.send "RS: Hello Client!"}
  ws.onmessage { 
            |msg| ws.send "RS: Pong: #{msg}" 
            puts msg 
           }
  ws.onclose   { puts "WebSocket closed" }
end

最佳答案

问题是 POCO 默认的 HTTP 请求版本是 1.0。

RFC 规范的第 4.1 节指出最小值为 1.1:

https://www.rfc-editor.org/rfc/rfc6455#section-4.1

  1. The method of the request MUST be GET, and the HTTP version MUST be at least 1.1. For example, if the WebSocket URI is "ws://example.com/chat", the first line sent should be "GET /chat HTTP/1.1".

通过以下方式替换 HTTPRequest 结构解决了问题:

HTTPRequest request(HTTPRequest::HTTP_GET, "/ws", "HTTP/1.1" );

干杯!

关于c# - POCO 1.5.1 Websocket 客户端无法连接到 c# websocket 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20181080/

相关文章:

c# - 简单的注入(inject)器在运行时更改注册

c# - 将图像添加到 Windows 窗体,以便它们在发布后正常工作

c# - NetServerEnum 从哪里获取机器列表?

c# - 为什么事件会为空? (你调用的对象是空的)

javascript - 获取事件内部发送的信息(event.data)JQuery/Websocket

rust - 如何设置 actix websocket actorless 的消息大小限制?

c++ - Qt 从旧的 C++ 项目生成 .ui 文件?

c++ - 集成多态可扩展排序顺序

c++ - 您如何在 Visual Studio 中生成具有随机输出名称的 PDB 文件?

java - spring websockets - 是否可以向离线用户发送消息?