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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘