构建了一个实时应用,部署在服务器上面,但是每次运行一段时间后socket.io就报错了,错误是这样的
前面都是正常的,某一时刻突然就不行了,重启node才行,经常这样
socket.io版本是1.3.5,node版本是0.10.29,node代码是这样的,
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io').listen(http);
var user = {},
bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.post('/test', function(req, res) {
var msgBody = JSON.parse(req.param('message'));
console.log(msgBody);
console.log(new Date());
io.emit('chat message', msgBody);
res.send('hello world');
console.log(new Date());
});
io.on('connection', function(socket) {
console.log('connecting');
socket.on('chat message', function(msg) {
console.log(msg);
io.emit('chat message', msg);
});
});
http.listen(4012, function() {
console.log('listening on *:4012');
});
客户端的代码如下:
var socket = io.connect('http://localhost:4012');
第一次发帖。。实在不知道怎么办,求帮助,万分感谢!
做过一个聊天应用,给你参考参考:https://github.com/yuanzm/Live-Chat