socket.io 不发送给客户端 [英] socket.io not sending to client

查看:33
本文介绍了socket.io 不发送给客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的脚本,以便在每次更新文件时将数据从文件发送到客户端.我已经测试过,发现文件被读取了,但是客户端没有收到任何东西.控制台中没有错误.我是 socket.io 的新手.

I am trying to create a simple script to send data from a file every to the client every time the file is updated. I have tested and found that the file is read, but the client doesn't receive anything. there are no errors in the console. I am fairly new to socket.io.

node.js 代码

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var fs = require("fs");
var port = process.env.PORT || 3000;

app.get('/', function(req, res){
    res.sendFile(__dirname + '/popup.html');
});

fs.watchFile("assets/popup.json", {interval:100}, function(curr, prev)
{
    fs.readFile("assets/popup.json",{encoding:"utf8"}, function(err, data){
        io.emit("popup", data)
    })
});
http.listen(port, function(){
    console.log('listening on *:' + port);
});

客户端代码

var socket = io();
socket.on('popup', function(msg){
    alert("hello")
});

推荐答案

当事情不像这样正常工作时,您需要求助于调试模式".在这种模式下,您需要收集可能发生的所有可能事件,然后看看从中学到了什么.为此,将此代码添加到客户端:

Whenever things aren't working right like this, you need to resort to "debug mode". In that mode, you need to gather all possible events that might be happening and see what you learn from that. To that end, add this code to the client:

var socket = io();
socket.on('popup', function(msg){
    console.log("hello: ", msg)
});
socket.on('connection', function() {
    console.log("client connected");
});

socket.on('connect_error', function(err) {
    console.log("client connect_error: ", err);
});

socket.on('connect_timeout', function(err) {
    console.log("client connect_timeout: ", err);
});

这些消息都记录在 socket.io Github 站点,您可以随时通过谷歌搜索socket.io github"找到.

These messages are all documented in the client-side doc on the socket.io Github site which you can find by Googling "socket.io github" at any time.

然后,查看页面加载时您在浏览器控制台中看到的内容.如果您不知道如何在您使用的任何浏览器中打开浏览器控制台,请使用谷歌进行查找.您需要在页面加载时查看调试控制台.

Then, see what you see in the browser console when the page loads. If you don't know how to open the browser console in whichever browser you are using, google it to find out. You need to be looking at the debug console when the page loads.

仅供参考,我们假设您已在此代码之前通过脚本标记将 socket.io 加载到页面中.如果没有,该错误也会显示在控制台中.

FYI, we're assuming that you've loaded socket.io into the page via a script tag before this code. If not, that error will show in the console too.

然后 OP 收到此错误:

The OP then gets this error:

client connect_error: 
    Error: server error at Socket.onPacket (socket.io-1.2.0.js:1) 
      at XHR.<anonymous> (socket.io-1.2.0.js:1) 
      at XHR.Emitter.emit (socket.io-1.2.0.js:1) 
      at XHR.Transport.onPacket (socket.io-1.2.0.js:1) 
      at callback (socket.io-1.2.0.js:2) 
      at Object.exports.decodePayload (socket.io-1.2.0.js:2) 
      at XHR.Polling.onData (socket.io-1.2.0.js:2) 
      at Request.<anonymous> (socket.io-1.2.0.js:2) 
      at Request.Emitter.emit (socket.io-1.2.0.js:1) 
      at Request.onData (socket.io-1.2.0.js:2)

<小时>

好的,进展.你是如何在客户端页面加载socket.io的?这似乎是因为您在客户端和服务器中的 socket.io 版本不匹配.你应该这样做:


OK, progress. How are you loading socket.io in the client page? This seems like it might be that you have mismatched versions of socket.io in client and server. You should be doing:

<script src="/socket.io/socket.io.js"></script>

然后您的服务器将向客户端页面提供与 socket.io 完全相同的版本.还有,既然这个错误报客户端socket.io 1.2.0,那么服务器上安装的是什么版本的socket.io?

and then your server will be feeding the client page the exact same version of socket.io. Also, since this error reports client-side socket.io 1.2.0, what version of socket.io is installed on the server?

这篇关于socket.io 不发送给客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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