Reputation: 18939
I created a express 3 app with the express generator and installed socket.io. On app.js im emiting a message:
io.sockets.on('connection', function(socket) {
socket.emit('init', { msg: 'Welcome'});
});
At server side I wrote:
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
<script src='/socket.io/socket.io.js' />
<script>
var socket = io.connect('http://127.0.0.1:3000');
socket.on('init', function (data) {
console.log(data.msg);
});
</script>
</head>
<body>
<h1><%= title %></h1>
<p>Welcome to <%= title %></p>
</body>
</html>
If I run app.js It should print "Welcome" on the console, but its not priting anything. I checked if /socket.io/socket.io.js is accesed and it does.
When running the app I get:
info - socket.io started
Express server listening on port 3000
GET / 200 28ms - 472
GET /stylesheets/style.css 200 163ms - 110
debug - served static content /socket.io.js
Am I missing something? I followed the socket.io webpage examples, but it seems that server is running fine... maybe something at the client-side?
EDIT: I also tried var socket = io.connect('http://127.0.0.1', { port: 3000 } );
on the client side, and also running all socket client side from the body.
Doing a console.log on the io.sockets.on event gave nothing... so "connection" is never reached.
Upvotes: 3
Views: 11239
Reputation: 3883
adding a index.jade file to the example I posted before
server.js
var app = require('express')()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server)
server.listen(3000)
io.set('loglevel',10) // set log level to get all debug messages
io.on('connection',function(socket){
socket.emit('init',{msg:"test"})
})
app.get('/',function(req,res){
res.render('index.jade')
})
/views/index.jade
doctype html
html
head
script(src="/socket.io/socket.io.js")
script.
var sockets = io.connect()
sockets.on('init',function(msg){
alert(msg.msg)
})
Upvotes: 1
Reputation: 16395
app.js:
var express = require('express')
, routes = require('./routes')
, http = require('http')
, path = require('path');
var app = express();
var server = http.createServer(app)
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.get('/', routes.index);
server.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
var io = require("socket.io").listen(server)
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
});
index.html:
<script>
var socket = io.connect();
socket.on('news', function (data) {
console.log(data);
});
</script>
In your browser's console you should see an object containing "hello": "world".
Upvotes: 11