Permalink
Browse files

fixed shit

  • Loading branch information...
1 parent d28df3a commit b86a4d6be4e6a6300759689f95b9a84f20d3285a @AJKaufman committed Feb 17, 2017
Showing with 53 additions and 60 deletions.
  1. +53 −60 src/server.js
View
@@ -10,9 +10,9 @@ const port = process.env.PORT || process.env.NODE_PORT || 3000;
const index = fs.readFileSync(`${__dirname}/../client/client.html`);
const onRequest = (request, response) => {
- response.writeHead(200, { 'Content-Type': 'text/html' });
- response.write(index);
- response.end();
+ response.writeHead(200, { 'Content-Type': 'text/html' });
+ response.write(index);
+ response.end();
};
const app = http.createServer(onRequest).listen(port);
@@ -26,84 +26,77 @@ const io = socketio(app);
const users = {};
const onJoined = (sock) => {
- const socket = sock;
-
- socket.on('join', (data) => {
-
+ const socket = sock;
+
+ socket.on('join', (data) => {
// add a user object
- socket.name = data.name;
- users[socket.name] = socket.name;
-
+ socket.name = data.name;
+ users[socket.name] = socket.name;
+
// message back to new user
- const joinMsg = {
- name: 'server',
- msg: `There are ${Object.keys(users).length} users online`,
- };
+ const joinMsg = {
+ name: 'server',
+ msg: `There are ${Object.keys(users).length} users online`,
+ };
- socket.name = data.name;
- socket.emit('msg', joinMsg);
+ socket.name = data.name;
+ socket.emit('msg', joinMsg);
- socket.join('memeHouse');
+ socket.join('memeHouse');
// announcement to everyone in the room
- const response = {
- name: 'server',
- msg: `${data.name} has joined the room.`,
- };
- socket.broadcast.to('memeHouse').emit('msg', response);
+ const response = {
+ name: 'server',
+ msg: `${data.name} has joined the room.`,
+ };
+ socket.broadcast.to('memeHouse').emit('msg', response);
- console.log(`${data.name} joined`);
+ console.log(`${data.name} joined`);
// success message back to new user
- socket.emit('msg', {name: 'server', msg: 'You joined the room' });
-
- });
+ socket.emit('msg', { name: 'server', msg: 'You joined the room' });
+ });
};
const onMsg = (sock) => {
- const socket = sock;
-
-
-
- socket.on('diceroll', () => {
- let roll = Math.random() * 6;
- io.sockets.in('memeHouse').emit('msg', { name: 'server', msg: `${socket.name} rolled a ${roll}`});
- });
-
- socket.on('msgToServer', (data) => {
- io.sockets.in('memeHouse').emit('msg', { name: socket.name, msg: data.msg + data.catchphrase, color: data.color });
- });
-
+ const socket = sock;
+
+
+ socket.on('diceroll', () => {
+ const roll = Math.random() * 6;
+ io.sockets.in('memeHouse').emit('msg', { name: 'server', msg: `${socket.name} rolled a ${roll}` });
+ });
+
+ socket.on('msgToServer', (data) => {
+ io.sockets.in('memeHouse').emit('msg', { name: socket.name, msg: data.msg + data.catchphrase, color: data.color });
+ });
};
const onDisconnect = (sock) => {
- const socket = sock;
-
+ const socket = sock;
+
// remove a user from the user object
- socket.on('disconnect', (data) => {
+ socket.on('disconnect', () => {
// announcement to everyone in the room
- const response = {
- name: 'server',
- msg: `${socket.name} has left the room.`,
- };
-
- socket.broadcast.to('memeHouse').emit('msg', response);
- console.log(`${socket.name} left`);
- socket.leave("memeHouse");
-
- delete users[socket.name];
- });
+ const response = {
+ name: 'server',
+ msg: `${socket.name} has left the room.`,
+ };
+
+ socket.broadcast.to('memeHouse').emit('msg', response);
+ console.log(`${socket.name} left`);
+ socket.leave('memeHouse');
+ delete users[socket.name];
+ });
};
io.sockets.on('connection', (socket) => {
- console.log('started');
-
- onJoined(socket);
- onMsg(socket);
- onDisconnect(socket);
+ console.log('started');
+
+ onJoined(socket);
+ onMsg(socket);
+ onDisconnect(socket);
});
console.log('Websocket server started');
-
-

0 comments on commit b86a4d6

Please sign in to comment.