Skip to content

Commit

Permalink
always use ws
Browse files Browse the repository at this point in the history
  • Loading branch information
atabel committed Nov 3, 2016
1 parent cefdcc2 commit 5c839b9
Showing 1 changed file with 14 additions and 24 deletions.
38 changes: 14 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const http = require('http');
const url = require('url');
const WebSocketServer = require('ws').Server;
const createGoogleTokenVerifier = require('./google-auth/google-id-token-verifier');
const SERVER_PORT = 8080;
const GOOGLE_CLIENT_ID = require('./config').GOOGLE_CLIENT_ID;
const SERVER_PORT = process.env.PORT || 8080;

const verifyAuthToken = createGoogleTokenVerifier({clientId: GOOGLE_CLIENT_ID});

Expand Down Expand Up @@ -44,11 +43,23 @@ const send = event => {
});
};

const handleInServer = event => {
if (event.type === 'getUsers') {
users.forEach(user => {
send({type: 'user', sender: 'server', receiver: event.sender, payload: user});
});
}
};

const handleEvent = fromUser => eventJson => {
const event = JSON.parse(eventJson);
event.sender = fromUser.id;
console.log(`< ${fromUser.email}`, event);
send(event);
if (event.receiver === 'server') {
handleInServer(event);
} else {
send(event);
}
};

wss.on('connection', ws => {
Expand All @@ -59,25 +70,4 @@ wss.on('connection', ws => {
send({type: 'user', sender: user.id, receiver: 'all', payload: user});
});

http.createServer(function (req, res) {
// Set CORS headers
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Request-Method', '*');
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
// res.setHeader('Access-Control-Allow-Headers', req.header.origin);
if (req.method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}
const path = url.parse(req.url).pathname;
if (path === '/getUsers') {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({users}));
} else {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('not found :(');
}
}).listen(8000);

console.log('listening on port ', SERVER_PORT);

0 comments on commit 5c839b9

Please sign in to comment.