0
獲取socketid我有以下app.js發送給啓動請求socket.io的特定客戶端。在路由器
var express = require('express');
var app = express();
// var responseHandlerRouter = require('./routes/responseHandlerRouter.js');
routes = require('./routes');
var server = app.listen(3000, function() {
console.log('Example app listening on port 3000!');
});
io = require('/usr/local/lib/node_modules/socket.io').listen(server);
app.use('/', routes(io));
routes.js
var express = require('express');
var router = express.Router();
module.exports = function (io) {
// all of this router's configurations
router.get('/login', function (req, res, next) {
io.emit('notification', 'news');
res.end('well finally I am here');
});
return router;
}
的index.html
<!doctype html>
<html>
<head>
<script src="http://127.0.0.1:3000/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:3000/login');
socket.on('notification', function (data) {
console.log(data);
});
</script>
</head>
<body>
<ul id='messages'></ul>
</body>
</html>
當我做了得到的網址,它發出的嚴格g'新聞'給瀏覽器,但它發送給所有的客戶端。如果我打開多個瀏覽器,它將發送給所有的瀏覽器。我如何發送給特定的客戶?或者如何在路由器中獲取socketid。可以使用io對象嗎?