Skip to content

Instantly share code, notes, and snippets.

@rdemarco
Created May 3, 2013 00:33
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 rdemarco/5506473 to your computer and use it in GitHub Desktop.
Save rdemarco/5506473 to your computer and use it in GitHub Desktop.
Here is the app.js for the demo i'm trying to upload to nodejitso
// Including libraries
var app = require('express').createServer(handler),
io = require('socket.io').listen(app),
static = require('node-static'); // for serving files
var path = require("path");
// This will make all the files in the current folder
// accessible from the web
var fileServer = new static.Server('/');
// usernames which are currently connected to the chat
var usernames = {};
// This is the port for our web server.
// you will need to go to http://localhost:8080 to see it
app.listen(80);
// If the URL of the socket server is opened in a browser
function handler (request, response) {
request.addListener('end', function () {
fileServer.serve(request, response); // this will return the correct file
});
}
// Delete this row if you want to see debug messages
io.set('log level', 1);
// Listen for incoming connections from clients
io.sockets.on('connection', function (socket) {
// Start listening for mouse move events
socket.on('mousemove', function (data) {
// This line sends the event (broadcasts it)
// to everyone except the originating client.
socket.broadcast.emit('moving', data);
});
socket.on('sendchat', function (data) {
// we tell the client to execute 'updatechat' with 2 parameters
io.sockets.emit('updatechat', socket.username, data);
});
socket.on('adduser', function(username){
// we store the username in the socket session for this client
socket.username = username;
// add the client's username to the global list
usernames[username] = username;
// echo to client they've connected
socket.emit('updatechat', 'SERVER', 'you have connected');
// echo globally (all clients) that a person has connected
socket.broadcast.emit('updatechat', 'SERVER', username + ' has connected');
// update the list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment