Node.js WebSocket 服务器

发布于 2024-11-03 06:55:01 字数 1802 浏览 1 评论 0原文

目前,我尝试为我们的数据库周围的新活动创建一个推送服务器实例。当然,您会找到很多有关该主题的信息。

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

使用以下客户端实现:

<!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)

我不知道问题所在。

感谢您的帮助。

问候!

currently i try to create a push server instance for new activities around our database. Of course, you find a lot of information about this topic.

I'm using:
http://static.brandedcode.com/nws-docs/#s6-p1

With the following client implementation:

<!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>

The console returns:

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)

I don't know the problem.

Thanks for your help.

Greets!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温馨耳语 2024-11-10 06:55:01

您正在尝试使用 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 支持 WebSocket。在其他浏览器中测试将会失败。

You´re trying to connect directly to a WebSocket server using Socket.io.
If you are running only a WebSocket server and not the Socket.io server, the you can use the normal HTML5 API to connect to websockets.

for example:

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

What browser are you using?
WebSockets are currently only supported by Google Chrome. Tests in other browsers will fail.

情话难免假 2024-11-10 06:55:01

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

You probably wanted 'ws://push.xxx.binder.test' instead of 'ws//push.xxx.binder.test' (missing colon).

少年亿悲伤 2024-11-10 06:55:01

更改

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

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

You no need to add prefix for your domain for socket.io (尤其是斜杠前没有冒号)。另外 var webSocket 也不是一个好的命名 - socket.io 不仅可以使用 websockets (即使在你的错误中它也使用 xhr-poliing )

change

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

to

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

You no need to add prefix for your domain for socket.io (especially without colon before slashes). Also var webSocket isn't good naming - socket.io can use not only websockets (even in your errors it using xhr-poliing)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文