socket.io 客户端“发出”在 chrome 控制台中显示一个奇怪的错误
我正在 Windows 上使用当前最新版本的 nodejs + socket.io 开发一个简单的应用程序。当在客户端创建套接字时,我遇到了一个问题,但是 socket.emit 在控制台中产生以下输出(使用 Google Chrome):
GET http://0.0.34.184:8888/socket.io/1/?t=1321385855665&jsonp=0
这很奇怪,因为我正在 localhost:8888 上进行测试,所以它应该位于至少 127.0.0.1 而不是 0.0.34.184。页面上的 socket.io 客户端是从标记中的服务器请求的,如下所示:
<script type="text/javascript" src="socket.io/socket.io.js"></script>
然后我使用以下代码进行连接:
socket = io.connect('localhost:8888');
socket.on("my_event", receiveNetEvent);
console.log('LOG: sio init complete, socket=' + socket);
日志指出 socket=[Object object] 不为 null 或未定义,因此一切都应该正常。
发送本身是在以下函数中执行的:
function sendRequest(request){
socket.emit('request', request);
}
传递给函数的请求对象是这样形成和传递的:
var initRequest = {};
initRequest.subject = 'weather';
initRequest.key = 'initial';
sendRequest(initRequest);
并且它是一个有效的 json 对象,同样,根据 console.log()
。
所有这一切让我完全不知道发生这种情况的任何可能原因。
I am developing a simple application using the current latest versions of nodejs + socket.io on Windows. I have encountered a problem when the socket get created on the client side, but socket.emit results in the following output in the console (using Google Chrome):
GET http://0.0.34.184:8888/socket.io/1/?t=1321385855665&jsonp=0
Which is quite strange because I am testing on localhost:8888 so it should be at least 127.0.0.1 and not 0.0.34.184. The socket.io client on the page is requested from the server in the tag as follows:
<script type="text/javascript" src="socket.io/socket.io.js"></script>
Then I connect using the following code:
socket = io.connect('localhost:8888');
socket.on("my_event", receiveNetEvent);
console.log('LOG: sio init complete, socket=' + socket);
The log states that socket=[Object object] which is not null or undefined so everything should work.
The sending itseld is performed in the following function:
function sendRequest(request){
socket.emit('request', request);
}
The request object, passed to the function is formed and passed like this:
var initRequest = {};
initRequest.subject = 'weather';
initRequest.key = 'initial';
sendRequest(initRequest);
And it is a valid json object according, again, to console.log()
.
All this leaves me totally clueless about any possible reason of the happening.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了同样的问题,但我有不同的原因。我的设置是在端口 9000 的 tomcat 服务器上运行套接字客户端,并连接到运行 nodejs 的 localhost:80,我没有意识到该端口是强制性的,即使默认情况下是 80。
所以我给了端口并替换了
因为我的两个服务器都指向同一个域,在本例中为 localhost...socket io 由于某种原因是占用端口 9000
希望它对某人有帮助。
I faced the same issue but I had a different reason. My setup was running the socket client on a tomcat server with port 9000 and was connecting to localhost:80 running nodejs, what i didn't realize is the port was mandatory even if it was 80 by default.
SO i gave the port and replaced
Because both my servers were pointing to same domain which in this case localhost... socket io for some reason was taking port 9000
Hope it helps somebody.
尝试将 http:// 添加到连接路径。这为我解决了这个错误。
Try adding http:// to the connect path. That solved this error for me.
我也面临同样的问题,但通过仅强制 xhr 轮询服务器端 javascript 解决了这个问题,结果成功了。
I was also facing the same problem but resolved it by forcing only xhr-polling to server side javascript and that worked out.