簡體   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