Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0e3e5a
commit 1c1891e
Showing
3 changed files
with
58 additions
and
10 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,41 @@ | ||
import Router from '@koa/router'; | ||
import Koa from 'koa'; | ||
import {Server} from 'socket.io'; | ||
import {Server, Socket} from 'socket.io'; | ||
|
||
import {createServer} from 'http'; | ||
|
||
type Connection = { | ||
socket: Socket; | ||
}; | ||
|
||
const ingestMatcher = /^\/ingest\/([^/]+)$/; | ||
|
||
const connections: Record<string, Connection> = {}; | ||
|
||
const app = new Koa(); | ||
const server = createServer(app.callback()); | ||
|
||
const wss = new Server(server); | ||
|
||
wss.of('/ingest').on('connection', socket => { | ||
socket.on('store-init', console.log); | ||
socket.on('store-update', console.log); | ||
const router = new Router(); | ||
|
||
router.get('/stats', ctx => { | ||
ctx.body = `Current number of clients: ${Object.keys(connections).length}`; | ||
}); | ||
|
||
console.log('here we gooo'); | ||
app.use(router.routes()).use(router.allowedMethods()); | ||
|
||
// Connect | ||
wss.of(ingestMatcher).on('connection', socket => { | ||
const key = socket.nsp.name.match(ingestMatcher)[1]; | ||
|
||
connections[key] = { | ||
socket, | ||
}; | ||
|
||
app.use(ctx => { | ||
ctx.body = 'Hello World'; | ||
socket.on('disconnect', () => delete connections[key]); | ||
|
||
socket.on('store-init', console.log); | ||
socket.on('store-update', console.log); | ||
}); | ||
|
||
server.listen(8888); |
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
1c1891e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: