var app = require('express');
var express = app();
var server = require('http').createServer(express);
var io = require('socket.io').listen(server);
//This config is mandatory for heroku
io.configure(function (){
io.set("log level",1);
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
var allowCrossDomain = function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
}
express.configure(function () {
express.set('port', process.env.PORT || 8080);
express.use(allowCrossDomain);
express.use("/", app.static("./wwwroot/"));
});
io.on('connection', function(socket) {
console.log(socket.id + " is connected");
socket.emit('connected', 'You welcome!');
});
server.listen(express.get('port'));
console.log("Express server is listening on port " + express.get('port'));
<script src="./js/socket.io.js"/>
<script src="cordova.js"/>
<script>
var socket;
document.addEventListener('deviceReady',ondeviceReady,false);
function onDeviceReady(){
connect();
}
function connect(){
socket = io.connect('http://testapp.herokuapp.com');
socket.on('connected',function(){
alert("connected ! ");
});
}
</script>For android you have to edit cordova.xml and add access to the socketio host:
<access origin="HOST*"/>
See Faq:
For android you have to edit cordova.xml and add access to the socketio host: index.html (with socketio example): |
app.js (server side javascript / basic socketio example):
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});The HOST you have to replace with hostname of your socket.io server!