Tcp C+上的Websocket+;Javascript没有响应

Tcp C+上的Websocket+;Javascript没有响应,tcp,websocket,broadcasting,Tcp,Websocket,Broadcasting,我想要一个简单的广播C/C++服务器,它将接收客户端的消息并将它们发送回所有其他连接的部分,包括发送方。我有以下代码: 服务器端(C++): #include <boost/asio.hpp> #include <boost/bind.hpp> #include <boost/thread.hpp> #include <iostream> #include <memory> #include <set> #include

我想要一个简单的广播C/C++服务器,它将接收客户端的消息并将它们发送回所有其他连接的部分,包括发送方。我有以下代码:

服务器端(C++):

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>

#include <iostream>
#include <memory>
#include <set>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>

typedef websocketpp::server<websocketpp::config::asio> server;

using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;

class broadcast_server {
public:
    broadcast_server() {
        m_server.init_asio();

        m_server.set_open_handler(bind(&broadcast_server::on_open,this,::_1));
        m_server.set_close_handler(bind(&broadcast_server::on_close,this,::_1));
        m_server.set_message_handler(bind(&broadcast_server::on_message,this,::_1,::_2));
    }

    void on_open(connection_hdl hdl) {
        m_connections.insert(hdl);
    }

    void on_close(connection_hdl hdl) {
        m_connections.erase(hdl);
    }

    void on_message(connection_hdl hdl, server::message_ptr msg) {
        for (auto it : m_connections) {
            m_server.send(it,msg);
        }
    }

    void run(uint16_t port) {
        m_server.listen(port);
        m_server.start_accept();
        m_server.run();
    }
private:
    typedef std::set<connection_hdl,std::owner_less<connection_hdl>> con_list;

    server m_server;
    con_list m_connections;
};

int main() {
    broadcast_server server;
    server.run(xxxx);
}
<html>
<head>
<title>Websocket chat example</title>
<style type="text/css">
html, body {
    font: normal 0.9em arial, helvetica;
}

#log {
    width: 600px;
    height: 300px;
    border: 1px solid #7F9DB9;
    overflow: auto;
}

#msg {
    width: 400px;
}
</style>
<script type="text/javascript">
    var socket;

    function init() {
        var host = "ws://xxx.xxx.xxx.xxx:xxxx/echobot";
        try {
            socket = new WebSocket(host);
            log('WebSocket - status ' + socket.readyState);
            socket.onopen = function(msg) {
                log("Welcome - status " + this.readyState);
            };
            socket.onmessage = function(msg) {
                log("Received: " + msg.data);
            };
            socket.onclose = function(msg) {
                log("Disconnected - status " + this.readyState);
            };
        } catch (ex) {
            log(ex);
        }
        $("msg").focus();
    }

    function send() {
        var txt, msg;
        txt = $("msg");
        msg = txt.value;
        if (!msg) {
            alert("Message can not be empty");
            return;
        }
        txt.value = "";
        txt.focus();
        try {
            socket.send(msg);
            log('Sent: ' + msg);
        } catch (ex) {
            log(ex);
        }
    }
    function quit() {
        if (socket != null) {
            log("Goodbye!");
            socket.close();
            socket = null;
        }
    }

    function reconnect() {
        quit();
        init();
    }

    // Utilities
    function $(id) {
        return document.getElementById(id);
    }
    function log(msg) {
        $("log").innerHTML += "<br>" + msg;
    }
    function onkey(event) {
        if (event.keyCode == 13) {
            send();
        }
    }
</script>

</head>
<body onload="init()">
    <h3>WebSocket v2.00</h3>
    <div id="log"></div>
    <input id="msg" type="textbox" onkeypress="onkey(event)" />
    <button onclick="send()">Send</button>
    <button onclick="quit()">Quit</button>
    <button onclick="reconnect()">Reconnect</button>
</body>
</html>

无论如何,客户端似乎没有从广播服务器接收任何内容。服务器可以完美地处理多个连接,但客户端不会输出其答案。

如果您还没有,请确保通信正常,例如使用wireshark,一种非常优秀的网络嗅探软件。谢谢您,Müller。没错,这是我NAT的一个问题。我试着用我的4G互联网连接到HTML,它工作得很好,但是如果我试着用我的WAN或LAN ip连接到我自己,它就不起作用了,尽管它使用的是简单的“localhost”。然后这就成了StackOverflow的话题,因为它是关于正确管理NAT路由器的,或者足够自由地设置防火墙。如果您还没有,请确保通信正常,例如使用wireshark,一种非常优秀的网络嗅探软件。谢谢您,Müller。没错,这是我NAT的一个问题。我试着用我的4G互联网连接到HTML,它工作得很好,但是如果我试着用我的WAN或LAN ip连接到我自己,它就不起作用了,尽管它使用的是简单的“localhost”。然后这就成了StackOverflow的话题,因为它是关于正确管理你的NAT路由器,或者足够允许地设置你的防火墙。
[2015-07-11 03:59:27] [frame_header] Dispatching write containing 1 message(s) containing 2 header bytes and 12 payload bytes
[2015-07-11 03:59:27] [frame_header] Header Bytes: 
[0] (2) 81 0C 

[2015-07-11 03:59:27] [frame_payload] Payload Bytes: 
[0] (12) [1] Test_message