Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
736 views
in Technique[技术] by (71.8m points)

javascript - socket.io not sending to client

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 code

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);
});

client code

var socket = io();
socket.on('popup', function(msg){
    alert("hello")
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
});

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.

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.


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)

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>

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...