-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
50 lines (34 loc) · 917 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import express from 'express'
import cors from 'cors'
import { AddressInfo } from 'net'
import { createServer } from 'http'
const app = express()
const server = createServer(app)
import socketio from 'socket.io'
app.use(cors())
app.use(express.json())
const io: socketio.Server = socketio(server)
io.origins('*:*')
io.on('connect', (socket) => {
console.log('New connection')
socket.emit('data', { bar: 'foo' })
})
io.on('connection', (socket) => {
setInterval(() => socket.send('Sent a message.'), 400)
socket.on('data', function (data) {
console.log(data)
socket.emit('data',JSON.stringify(data))
})
socket.on('disconnect', function () {
console.log('A user disconnected')
})
})
const port = 8080
const runServer = async () => {
server.listen(port, () => {
const addr = server.address() as AddressInfo
console.log(`Listening on port ${addr.port}`)
})
return server
}
runServer()