socket.io 客户端“发出”在 chrome 控制台中显示一个奇怪的错误

发布于 2024-12-15 17:27:16 字数 1068 浏览 0 评论 0原文

我正在 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 技术交流群。

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

发布评论

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

评论(3

后知后觉 2024-12-22 17:27:16

我遇到了同样的问题,但我有不同的原因。我的设置是在端口 9000 的 tomcat 服务器上运行套接字客户端,并连接到运行 nodejs 的 localhost:80,我没有意识到该端口是强制性的,即使默认情况下是 80。

所以我给了端口并替换了

io.connect('http://localhost') 和 io.connect('http://localhost: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

io.connect('http://localhost') with io.connect('http://localhost:80')

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.

任谁 2024-12-22 17:27:16

尝试将 http:// 添加到连接路径。这为我解决了这个错误。

socket = io.connect('http://localhost:8888');

Try adding http:// to the connect path. That solved this error for me.

socket = io.connect('http://localhost:8888');
灯角 2024-12-22 17:27:16

我也面临同样的问题,但通过仅强制 xhr 轮询服务器端 javascript 解决了这个问题,结果成功了。

    var io = require('socket.io').listen(8000);
    io.configure(function () {
      io.set('transports', ['xhr-polling']);
    });
<..Rest code..>

I was also facing the same problem but resolved it by forcing only xhr-polling to server side javascript and that worked out.

    var io = require('socket.io').listen(8000);
    io.configure(function () {
      io.set('transports', ['xhr-polling']);
    });
<..Rest code..>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文