socket.io没有发送到客户端 [英] socket.io not sending to client

查看:80
本文介绍了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网站,你可以通过Googling找到 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出现此错误:

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天全站免登陆