forked from wangdashuaihenshuai/crdt-edit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (33 loc) · 1.12 KB
/
index.js
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
const express = require('express')
const app = require('express')()
const http = require('http').Server(app)
const path = require('path')
const io = require('socket.io')(http)
const serverState = require('./server')
const { loadActionsInfo } = require('./lib')
app.use(express.static(path.join(__dirname, 'public')))
let allActions = []
io.on('connection', (socket) => {
const uid = serverState.uidManager.getId()
const timeStamp = serverState.timeStampManager.getTimeStamp()
const actionsInfo = JSON.stringify(allActions)
socket.emit('client-init', { uid, timeStamp, actionsInfo })
socket.on('client-actions', (actionsInfo) => {
const actions = loadActionsInfo(actionsInfo)
allActions = allActions.concat(actions)
for (let action of actions) {
serverState.addAction(action)
console.log('barodcast', action.toString())
socket.broadcast.emit('server-actions', actionsInfo)
}
})
socket.on('client-connect', (data) => {
socket.broadcast.emit('new message', {
})
})
socket.on('add user', (username) => {
})
})
http.listen(8888, function () {
console.log('listening on *:8888')
})