Node.js,WebSocket 位置问题? [英] Node.js, WebSocket Location Issue?

查看:30
本文介绍了Node.js,WebSocket 位置问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Node 在端口 8000 上创建了一个服务器,本地主机.验证它运行正常,但我无法访问客户端(Chrome 5)上的 WebSocket.尝试了各种 Git 存储库、node + websocket、socketIO、文章等的几种实现,什么都没有.

I created a server using Node listening on port 8000, localhost. Verified it's running properly, but I cannot access the WebSocket on the client (Chrome 5). Tried several implementations from various Git repos, node + websocket, socketIO, articles, etc. Nothing.

无端口冲突(sudo lsof -i tcp);试过 server.listen(8000, "*");指向 ws = new WebSocket("ws://:8000/test");Debian Lenny, Apache22节点 v0.1.98-31-g1c6671a

No port conflicts (sudo lsof -i tcp); Tried server.listen(8000, "*"); Pointed to ws = new WebSocket("ws://:8000/test"); Debian Lenny, Apache22 Node v0.1.98-31-g1c6671a

我认为可能与 url 重写有冲突.或者可能的权限.有什么想法吗?

I'm thinking there may be a conflict with url rewrite. Or possible permissions. Any ideas?

推荐答案

我在 Ubuntu 10.04 LTS 32 位和 Chrome 5.0.375.125 上遇到了类似的问题,并发现它是 Chrome 中的一个错误.以下是测试和解决问题的方法.我将 WebSocket 与 PHP 和更高版本的 Node.JS 一起使用:

I had a similar issue on Ubuntu 10.04 LTS 32-bit and Chrome 5.0.375.125 and found out it is a bug in Chrome. Here's how you can test and work around the problem. I used WebSocket with PHP and later Node.JS:

PHP:从 http 下载教程文件://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/ 并将其解压缩到 webroot 中名为socket"的文件夹中.该文件夹现在包含一个 readme.txt 以及文件夹server"和client".现在根据自述文件在控制台中启动脚本(对我来说命令是: sudo php -q/var/www/socket/server/startDaemon.php ).它应该打印开始监听 Socket"..让控制台窗口保持打开状态.现在转到 Chrome 中的 localhost/socket/client/client.php.它应该显示套接字状态 0",如果您查看控制台,您会看到没有新消息(没有建立连接).

PHP: Download a tutorial file from http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/ and extract it in a folder called 'socket' in your webroot. This folder now contains a readme.txt and the folders 'server' and 'client'. Now start the script in the console according to the readme (for me the command was: sudo php -q /var/www/socket/server/startDaemon.php ). It should print 'Start listening on Socket.' . Leave the console window open. Now go to localhost/socket/client/client.php in Chrome. It should say 'Socket Status 0' and if you look in the console you see no new messages (no connection was made).

现在诀窍来了:打开第二个 Chrome 标签页.将此选项卡指向相同的 url:localhost/socket/client/client.php(它还表示套接字状态 0").然后再次关闭它.您的原始选项卡现在应该显示套接字状态:1(打开)",并且在控制台中您会看到进行了握手.WebSocket 现在可以工作了.

Now here comes the trick: open a second Chrome tab. Point this tab to the same url: localhost/socket/client/client.php (It also says 'Socket Status 0'). And then close it again. Your original tab should now say 'Socket Status: 1 (open)' and in the console you see a handshake was made. WebSocket now works.

我重复了与上面相同的技巧,但这次使用的是 Node.JS 和 Socket.IO 脚本.Socket.IO-node ( github.com/LearnBoost/Socket.IO-node ) 中包含的聊天示例有同样的问题,挂在连接..."阶段.打开聊天框的第二个选项卡,然后再次关闭它解决了问题,聊天框继续正常加载.Node.JS 服务器在控制台中确认了连接.从那时起,WebSocket 运行良好.

I repeated the same trick as above but this time using Node.JS with the Socket.IO script. The chat example included in Socket.IO-node ( github.com/LearnBoost/Socket.IO-node ) had the same issue, hanging at the 'Connecting...' stage. Opening a second tab to the chat box and then closing it again solved the problem and the chat box proceeded to load properly. The Node.JS server confirmed the connection in the console. From then on WebSocket worked fine.

这篇关于Node.js,WebSocket 位置问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆