I want to emit some data to the client when some API route gets called.
I have to following code on server.js
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
io.on('connection', function(socket){
console.log('a user connected');
socket.emit('tx', 'msg');
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
Now I have this route /test:
var bsg = require('./routes/test');
And that file:
var express = require('express');
var passport = require('passport');
var router = express.Router();
router.get('/test', function(req, res) {
//work here
});
module.exports = router;
On client side:
<script type="text/javascript">
var socket = io();
socket.on('tx', function(data) {
console.log(data);
});
</script>
Whats the best solution for this?
Thanks!
Express 4 / socket.io 1.4.5
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…