大家好,我想弄清楚我的代码中的错误是什么,我的代码是关于 socket.io 和 redis pub/sub 这是我第一次尝试这个,希望你们能帮助我。
这是我的index.html
<!doctype html>
<html>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = new io.Socket();
socket.connect();
socket.on('connection', function (socket) {
console.log('Connected');
});
socket.on('disconnect', function (socket) {
console.log('Disconnected');
});
</script>
<center>
<h1>Test Page</h1>
</center>
</html>
这是我的app.js
var redis = require('redis');
var app = require('http').createServer();
var io = require('socket.io').listen(app);
var client = redis.createClient();
var pub = redis.createClient();
var sub = redis.createClient();
app.listen(1234);
io.sockets.on('connection', function (socket){
sub.on('subscribe', function (channel){
pub.publish('Pub','Test Message 1');
pub.publish('Pub','Test Message 2');
pub.publish('Pub','Test Message 3');
});
sub.on('message', function (channel, message) {
console.log(channel + ':' + message);
sub.unsubscribe();
pub.end();
sub.end();
});
sub.incr('Channel Test');
sub.incr('Pub');
});
我希望你能帮我修复这段代码。提前谢谢各位。