Last active
July 15, 2017 20:54
-
-
Save ndugger/d25d4686fb752213f88e6e45b1f112d2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import { Server } from 'uws'; | |
| import actions from './actions'; | |
| import messenger from './messenger'; | |
| import router from './router'; | |
| import state from './state'; | |
| const { PORT, TEST_PORT } = process.env; | |
| actions.register(); | |
| const port = PORT || TEST_PORT || 8080; | |
| const server = new Server({ port }); | |
| server.on('connection', socket => { | |
| socket.on('message', message => { | |
| const { session } = JSON.parse(message).data; | |
| if (!state.sessions.has(session)) { | |
| state.sessions.set(session, new Set()); | |
| } | |
| if (!state.sessions.get(session).has(socket)) { | |
| state.sessions.get(session).add(socket); | |
| } | |
| messenger.send(message); | |
| }); | |
| }); | |
| messenger.on('message', async message => { | |
| try { | |
| const { type, data } = JSON.parse(message); | |
| const action = router.find(type); | |
| await action(data); | |
| } | |
| catch (error) { | |
| const type = 'error'; | |
| const data = { content: error.message }; | |
| messenger.send(JSON.stringify({ type, data })); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment