nodejs代码问题
发布于 2年前 作者 yejinwei 938 次浏览

引用了 io= require(‘socket.io’) 但是 运行时提示 io.sockets.emit中的 io not defined。 client.connect(function(sessionId) { console.log(‘Trying to connect the railway real time service…’); io.sockets.emit('news’, { will: 'Trying to connect the railway real time service…’}); client.subscribe(TRAIN_MVT_HE_TOC, function(body, headers) { //console.log(prettyjson.render(JSON.parse(body))); var t = JSON.parse(body); //console.log(t[0].header.msg_type); console.log("recived train messages length:"+t.length);

4 回复

io = require("socket.io") 写在哪的?

我建议还是版聊, 说不定以后有人会搜到这篇帖子解决跟你一样的问题.

可以把完整代码贴出来么?

@neuront 嗯 嗯 也是, 事先说明,代码是缺了一点东西的: var prettyjson = require(‘prettyjson’), fs = require(‘node-fs’), io= require(‘socket.io’), StompClient = require(‘stomp-client’).StompClient; var TRAIN_MVT_HE_TOC = '/topic/TRAIN_MVT_HE_TOC’; var client = new StompClient('datafeeds.networkrail.co.uk’, 61618, '[email protected]’, 'Newyork%212+’, ‘1.0’);

client.connect(function(sessionId) { console.log(‘Trying to connect the railway real time service…’); io.sockets.emit('news’, { will: 'Trying to connect the railway real time service…’}); client.subscribe(TRAIN_MVT_HE_TOC, function(body, headers) { //console.log(prettyjson.render(JSON.parse(body))); var t = JSON.parse(body); //console.log(t[0].header.msg_type); console.log("recived train messages length:"+t.length);

var movementarraybn = new Array(); for(var i in t){

//console.log(t[i].header.msg_type[3]);

switch(t[i].header.msg_type[3]-‘0’){ case 1: var stream = fs.createWriteStream('activation.json’, {’flags’: 'a’}); stream.write(JSON.stringify(t[i])+"\n"); stream.end(); parse_ActMsg(t[i]); break;

case 2: parse_CanMsg(t[i]); break;

case 3: movementarray.push(t[i]); break;

case 4:

break;

case 5:

break;

case 6:

break;

case 7: parse_ChangeName(t[i]); break;

case 8: break; } }

updateTrainInfos(movementarray); });

client.on('CON NECTED’, function () { console.log(' on connection '); });

client.on('ERROR’,function(){ console.log(‘on error’); }) }) })

var prettyjson = require(‘prettyjson’), fs = require(‘node-fs’), io= require(‘socket.io’), StompClient = require(‘stomp-client’).StompClient; var TRAIN_MVT_HE_TOC = '/topic/TRAIN_MVT_HE_TOC’; var client = new StompClient('datafeeds.networkrail.co.uk’, 61618, '[email protected]’, 'Newyork%212+’, ‘1.0’);

client.connect(function(sessionId) { console.log(‘Trying to connect the railway real time service…’); io.sockets.emit('news’, { will: 'Trying to connect the railway real time service…’}); client.subscribe(TRAIN_MVT_HE_TOC, function(body, headers) { //console.log(prettyjson.render(JSON.parse(body))); var t = JSON.parse(body); //console.log(t[0].header.msg_type); console.log("recived train messages length:"+t.length);

var movementarraybn = new Array(); for(var i in t){

//console.log(t[i].header.msg_type[3]);

switch(t[i].header.msg_type[3]-‘0’){ case 1: var stream = fs.createWriteStream('activation.json’, {’flags’: 'a’}); stream.write(JSON.stringify(t[i])+"\n"); stream.end(); parse_ActMsg(t[i]); break;

case 2: parse_CanMsg(t[i]); break;

case 3: movementarray.push(t[i]); break;

case 4:

break;

case 5:

break;

case 6:

break;

case 7: parse_ChangeName(t[i]); break;

case 8: break; } }

updateTrainInfos(movementarray); });

client.on('CON NECTED’, function () { console.log(' on connection '); });

client.on('ERROR’,function(){ console.log(‘on error’); }) }) })

回到顶部