Created
June 22, 2012 14:26
-
-
Save majek/2973054 to your computer and use it in GitHub Desktop.
SockJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node_modules | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm install | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var sockjs = require('sockjs'); | |
var sockjs_opts = {sockjs_url: "http://cdn.sockjs.org/sockjs-0.3.min.js", disconnect_delay:100000}; | |
var sockjs_echo = sockjs.createServer(sockjs_opts); | |
sockjs_echo.on('connection', function(conn) { | |
conn.on('data', function(message) { | |
conn.write(message); | |
}); | |
}); | |
// 2. Express server | |
var app = express.createServer(); | |
sockjs_echo.installHandlers(app, {prefix:'/rt'}); | |
console.log(' [*] Listening on 0.0.0.0:9999' ); | |
app.listen(9999, '0.0.0.0'); | |
app.get('/', function (req, res) { | |
res.sendfile(__dirname + '/index.html'); | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html><head> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> | |
<script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script> | |
</head><body lang="en"> | |
<h1>SockJS Express example</h1> | |
<script> | |
var _socket; | |
var get_socket = function(){ | |
var socket = _socket; | |
if (socket){ | |
return socket; | |
} | |
socket = new SockJS('http://localhost:9999/rt', null, {debug:true}); | |
_socket = socket; | |
return socket; | |
} | |
var socket = get_socket(); | |
socket.onopen = function(){ | |
console.log('open'); | |
}; | |
socket.onmessage = function(){ | |
console.log('msg'); | |
}; | |
socket.onclose = function(){ | |
console.log('close'); | |
}; | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "sockjs-express", | |
"version": "0.0.0-unreleasable", | |
"dependencies": { | |
"express": "2.5.8", | |
"sockjs": "*" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment