Last active
January 22, 2017 16:37
-
-
Save superalsrk/ee2d105a56bd1ac1d1a6b046982a0f0b to your computer and use it in GitHub Desktop.
wrong usage of sokcet.io broadcast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//wrong usage of socket.io broadcat | |
//错误的socket.io广播使用实例 | |
var app = require('express')() | |
var server = require('http').createServer(app); | |
var io = require('socket.io')(server); | |
var log4js = require('log4js'); | |
var redis = require('redis') | |
log4js.configure({ | |
appenders: [ | |
{ type: 'console'}, | |
{ type: 'file', filename: 'monitor.log', category: 'monitor'}, | |
] | |
}) | |
var LOG = log4js.getLogger('monitor') | |
app.get('/', function(req, res) { | |
res.sendfile(__dirname + '/index.html'); | |
}) | |
io.on('connection', function(socket){ | |
console.log('connected') | |
socket.on('joinroom', function(key) { | |
console.log('JOIN ROOM:' + key) | |
socket.leaveAll() | |
var redisclient = redis.createClient({}) | |
redisclient.subscribe(key) | |
socket.join('room:' + key) | |
var handleMessage = function(channel, msg) { | |
console.log('Message:' + msg + ' on channel:' + channel) | |
try { | |
var emitBody = JSON.parse(msg); | |
//It's wrong, it should be socket.emit | |
io.sockets.in('room:' + key).emit('message', JSON.parse(msg)) | |
} catch (err) { | |
//It's wrong, it should be socket.emit | |
io.sockets.in('room:' + key).emit('message', msg) | |
} | |
} | |
redisclient.on('message', handleMessage) | |
socket.on('disconnecting', function() { | |
redisclient.unsubscribe(key) | |
redisclient.removeListener('message', handleMessage) | |
redisclient.quit() | |
}) | |
}) | |
}); | |
LOG.warn('Listening on port: 8857') | |
server.listen(8857); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Test Page</title> | |
<script src='/socket.io/socket.io.js'> </script> | |
<script src="//cdn.bootcss.com/jquery/1.12.2/jquery.min.js"></script> | |
<link href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"> | |
<script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> | |
</head> | |
<body> | |
<div class="container protocal-notice"> | |
</br> | |
<ul id="msgList"> | |
</ul> | |
</div> | |
<script> | |
var try_ws = function(channel) { | |
var host = '/' | |
var socket = io.connect(host,{ | |
reconnectionAttemptes: 3, | |
reconnectionDelay: 3e3, | |
transports: ["websocket"], | |
"force new connection": !0 | |
} | |
) | |
socket.on("connect", function() { | |
socket.emit("joinroom", channel, function(t) { | |
}) | |
}) | |
socket.on('message', function(data){ | |
//处理data里的内容 | |
//alert(JSON.stringify(data)) | |
try { | |
var nowStr = channel + ' @ ' + new Date() + ' ==> ' + JSON.stringify(data) | |
$("#msgList").prepend($('<li></li>').text(nowStr)) | |
} catch (err) { | |
var nowStr = new Date() + ' ==> ' + data | |
$("#msgList").prepend($('<li></li>').text(nowStr)) | |
} | |
}) | |
} | |
try_ws("c001") | |
try_ws("c001"); | |
try_ws("c001") | |
try_ws("c001") | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//login redis-cli | |
publish c001 testcase |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment