javascript作为客户端

javascript作为客户端

本文介绍了Websockets使用asio c ++库作为服务器,javascript作为客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 asio 库在C ++中编写了服务器代码。我知道服务器代码工作,因为我测试它与客户端也写在C + +和使用 asio

I have written server code in C++ using the asio library. I know that the server code works, because I tested it with a client also written in C++ and using asio.

问题是,对于客户端使用以下javascript代码,连接不会被接受。我立即看到在javascript客户端上的消息框 Connection closed ... ,在服务器上我看到这个奇怪的消息:

The problem is that with the following javascript code for client, the connection doesn't get accepted. I immediately see the message box Connection closed... on the javascript client, and on the server I see this strange message:

Data RECEIVED: <------ I print this line myself
GET / HTTP/1.1
Host: localhost:15562
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: http://localhost:63344
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Sec-WebSocket-Key: IidMJmdoGe4kYu0+1VlrvQ==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits

index.html - 连接只是立即关闭...几乎相同的代码

index.html - Connection just closes immediately... Almost same code as seen here

function WebSocketTest() {
    if ("WebSocket" in window) {
        var ws = new WebSocket("ws://localhost:15562");

        ws.onopen = function () {
            alert("Connection opened...");
        };

        ws.onmessage = function (evt) {
            alert("Message received...");
        };

        ws.onclose = function () {
            alert("Connection closed...");
        };

        ws.send("Hi, from the client");
        ws.send("Hi, from the client");
    }
}

server.cpp 这工作正常。与,将ws_magic_string定义为

Here i used Crypto's methods from project https://github.com/eidheim/Simple-WebSocket-Server, there defined ws_magic_string as

  const std::string ws_magic_string = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";

祝你好运。

这篇关于Websockets使用asio c ++库作为服务器,javascript作为客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:50