Doesn't work anyway, I really tried so many different ways but nothing works.. I want establish a realtime connection with socket.io to monitor a redis db to get every new added item on a list. Any ideas ?
app.js
/**
* Module dependencies.
*/
var express = require('express');
var http = require('http');
var path = require('path');
var crawler = require('crawler.js');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
//app.use(express.errorHandler());
}
app.get('/rootaccess',function(req, res){
res.render('crawlercontrol');
});
app.get('/crawler/monitor',function(req, res){
res.render('monitor');
});
app.get('/crawler/start',function(req,res){
crawler.start(res);
});
app.get('/crawler/stop',function(req,res){
crawler.stop(res);
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
var io = require('socket.io').listen(app.listen(3111));
io.sockets.on('connection', function (socket) {
console.log('hei socket.io');
socket.emit('message', { message: 'welcome to the chat' });
socket.on('send', function (data) {
io.sockets.emit('message', data);
});
});
index.html
<!DOCTYPE html>
<html>
<head>
<title>ROOT Monitor</title>
<link rel="stylesheet" href="/css/normalize.css">
<link rel="stylesheet" href="/css/foundation.css" />
<link rel="stylesheet" href="/css/font-awesome.min.css">
<script src="/js/libs/modernizr.js"></script>
</head>
<body>
<div id="monitor">
Monitor
</div>
</body>
<script src="/js/libs/jquery-1.10.2.js"></script>
<script src="/js/libs/foundation.min.js"></script>
<script src='/socket.io/socket.io.js' />
<script>
var socket = io.connect('http://localhost:3111');
socket.on('news', function (data) {
console.log(data);
});
</script>
</script>
</html>
At this moment the only Error which appears is that socket.io doesn't provide the socket.io.js for the client..
I don't know if this is it or not; it's just a hunch. In app.js
you tell the express app to listen on the assigned port in the app
object:
http.createServer(app).listen(app.get('port'), ...
But then when you create io
, you call app.listen()
again within the listen
method of the object require
returns.
var io = require('socket.io').listen(app.listen(3111));
My hunch is that perhaps you are forcing the app
server to listen twice which may be causing the issue, again just a hunch. If I am wrong tell me. If you really want to listen on port 3111, you could try this:
var io = require('socket.io').listen(3111);
See if that gets you anywhere.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.