简体   繁体   English

发射()不起作用[Node.js]

[英]emit() not working [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. 我订阅laravel 5 event [ update.group ] update.group并在触发事件后在控制台接收消息,但是在浏览器的客户端,我没有接收到任何消息。 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: 这是我的node.js文件:

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? 任何人都可以找到问题所在?

It's pretty obvious from your debug output: testasdsa is not valid JSON, so you cannot parse it as such. 从调试输出中很明显: testasdsa不是有效的JSON,因此您不能这样解析它。 You will need to change the code that publishes the messages so that it's properly JSON encoded. 您将需要更改发布消息的代码,以便正确进行JSON编码。

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" } . 由于看起来您期望对象具有eventdata属性,因此发布者将需要编写类似{ "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" } 然后只需确保发布者发出类似{ "event": "update.group", "data": "somedata" }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM