doupai1876 2015-07-24 21:31
浏览 124
已采纳

emit()不工作[Node.js]

I subscibe to laravel 5 event[channal] update.group and I recive message in console after I trigger event but on client side in browser I don't recive any message. Also after I trigger event I recive message in console and then node server stop working with message:

bash-4.2# node node.js
Listening on Port 3000
Message Recieved: testasdsa
/home/client/public_html/node_modules/ioredis/lib/parsers/javascript.js:216
        throw err;
              ^
SyntaxError: Unexpected token e
    at Object.parse (native)
    at Redis.<anonymous> (/home/client/public_html/node_modules/node.js:10:20)
    at Redis.emit (events.js:110:17)
    at Redis.exports.returnReply (/home/client/public_html/node_modules/ioredis/lib/redis/parser.js:79:16)
    at ReplyParser.<anonymous> (/home/client/public_html/node_modules/ioredis/lib/redis/parser.js:27:10)
    at ReplyParser.emit (events.js:107:17)
    at ReplyParser.send_reply (/home/client/public_html/node_modules/ioredis/lib/parsers/javascript.js:281:8)
    at ReplyParser.execute (/home/client/public_html/node_modules/ioredis/lib/parsers/javascript.js:210:14)
    at Socket.<anonymous> (/home/client/public_html/node_modules/ioredis/lib/redis/event_handler.js:90:22)
    at Socket.emit (events.js:107:17)
    at readableAddChunk (_stream_readable.js:163:16)
    at Socket.Readable.push (_stream_readable.js:126:10)
    at TCP.onread (net.js:538:20)

Here is my node.js file:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Redis = require('ioredis');
var redis = new Redis();
redis.subscribe('update.group', function(err, count) {
});
redis.on('message', function(channel, message) {
    console.log('Message Recieved: ' + message);
    message = JSON.parse(message);
    io.sockets.emit(channel + ':' + message.event, message.data);
});
http.listen(3000, function(){
    console.log('Listening on Port 3000');
});

And for client side:

    var socket = io.connect('127.0.0.1:3000');
    socket.on("update.group", function(message){
    // increase the power everytime we load test route
    console.log(message);
});

Anyone can find what is problem?

  • 写回答

1条回答 默认 最新

  • doushaizhen1244 2015-07-24 21:41
    关注

    It's pretty obvious from your debug output: testasdsa is not valid JSON, so you cannot parse it as such. You will need to change the code that publishes the messages so that it's properly JSON encoded.

    Since it looks like you're expecting event and data properties on an object, the publisher would need to be writing something like { "event": "foo", "data": "testasdsa" }.

    To fix the problem of browser clients not getting the events, you need to change this in your server script:

    io.sockets.emit(channel + ':' + message.event, message.data);
    

    to:

    io.sockets.emit(message.event, message.data);
    

    Then just make sure the publisher sends out something like { "event": "update.group", "data": "somedata" }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题