Last active
August 29, 2015 14:03
-
-
Save jokecamp/e754a2b874341341b5eb to your computer and use it in GitHub Desktop.
Working Socket.io 1.0 with path option
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> | |
<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> |
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 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