Skip to content

Instantly share code, notes, and snippets.

@jokecamp
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jokecamp/e754a2b874341341b5eb to your computer and use it in GitHub Desktop.
Save jokecamp/e754a2b874341341b5eb to your computer and use it in GitHub Desktop.
Working Socket.io 1.0 with path option
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
</head>
<body>
<form action="">
<input type="input" id="m" /><button>Send</button>
</form>
<pre id="output"></pre>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script src="socket.io/socket.io.js"></script>
<script>
var socket = io('', { path: '/nfxplanner/socket.io/'});
socket.on('connection', function (data) {
console.log("connected to socket server");
});
socket.on('message', function(msg){
console.log('message recieved from server ' + msg);
$("#output").append(msg + "\n");
});
$('form').submit(function(){
var msg = $('#m').val() || "---";
console.log(socket);
socket.emit('message', "someone:" + msg);
console.log("message sent to server" + msg);
$('#m').val('');
return false;
});
</script>
</body>
</html>
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http, { serveClient: true, path: '/nfxplanner/socket.io/' });
app.get('/', function(req, res){
res.sendfile('index.html');
});
io.on('connection', function(socket){
console.log('a user connected!');
socket.on('disconnect', function(){
console.log('user disconnected!');
});
// emit incoming messages from client to Everyone!
socket.on('message', function(json){
io.emit('message', json);
});
});
http.listen(process.env.PORT || 8080, function(){
console.log('listening on ' + process.env.PORT || 8080);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment