繁体   English   中英

node.js WebSocket服务器

[英]node.js WebSocket Server

目前我尝试为我们数据库周围的新活动创建推送服务器实例。 当然,您可以找到有关此主题的大量信息。

我正在使用: http//static.brandedcode.com/nws-docs/#s6-p1

使用以下客户端实现:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

        <script src="http://cdn.socket.io/stable/socket.io.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>

        <title></title>
    </head>
    <body>
        <script type="text/javascript">
            (function() {
                var webSocket = new io.Socket('ws//test', {
                    port: 8080
                });

                webSocket.connect();

                webSocket.on('connect',function() {
                    console.log('Client has connected to the server!');
                });

                webSocket.on('message',function(data) {
                    console.log('Received a message from the server!',data);
                });

                webSocket.on('disconnect',function() {
                    console.log('The client has disconnected!');
                });

                window.ws = webSocket;
            }());
        </script>
    </body>
</html>

控制台返回:

Unexpected response code: 404
XMLHttpRequest cannot load http://ws//test:8080/socket.io/xhr-polling//1303822796984. Origin http://test is not allowed by Access-Control-Allow-Origin.
1303822796984GET http://ws//test:8080/socket.io/xhr-polling//1303822796984 undefined (undefined)

我不知道这个问题。

谢谢你的帮助。

映入眼帘!

您尝试使用Socket.io直接连接到WebSocket服务器。 如果您只运行WebSocket服务器而不运行Socket.io服务器,则可以使用普通的HTML5 API连接到websockets。

例如:

var ws = new WebSocket("ws://domain:port");
ws.onopen = function(){}
ws.onmessage = function(m){}
ws.onclose = function(){}

你使用的是什么浏览器? 目前,Google Chrome仅支持WebSockets。 其他浏览器中的测试将失败。

你可能想要'ws://push.xxx.binder.test'而不是'ws//push.xxx.binder.test' (缺少冒号)。

更改

var webSocket = new io.Socket('ws//push.xxx.binder.test', {

var webSocket = new io.Socket('push.xxx.binder.test', {

您无需为您的域添加socket.io的前缀(特别是在斜杠之前没有冒号)。 var webSocket是很好的命名 - socket.io不仅可以使用websockets (即使你的错误使用xhr-poliing

暂无
暂无

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

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