@superwhiskers/

undertale-textchat-thing

Express

No description

fork
loading
Files
  • index.js
  • views
  • package-lock.json
  • package.json
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const app = require('express')()
const debugmod = require('debug')
const debug = debugmod('server')
const server = require('http').createServer(app)
const io = require('socket.io')(server)
const shajs = require('sha.js')

let clientdata = {}

app.get('/', (req, res) => {

  res.sendFile(__dirname + "/views/index.html")
  debug('sent webpage')

})

io.on('connection', socket => {

  debug('someone has connected to the socket')
  var sid = socket.id

  for (id in clientdata) {

    if (clientdata[id] === undefined) {

      continue

    }

    socket.emit('textupdate', JSON.stringify({
      'text': clientdata[id].lasttext,
      'id': shajs('sha256').update(id).digest('hex')
    }))

  }

  socket.on('textupdate', sdata => {

    data = JSON.parse(sdata)
    var time = Date.now()

    if (clientdata[socket.id] !== undefined) {

      if (clientdata[socket.id].lasttext === data['text']) {

        return

      }

      clientdata[socket.id].lastcount = time
      clientdata[socket.id].lasttext = data['text']

    } else {

      clientdata[socket.id] = {
        lastcount: time,
        lasttext: data['text'],
      }

    }

    debug('client %s changed their message to "%s"', socket.id, data['text'])
    data['id'] = shajs('sha256').update(socket.id).digest('hex')
    socket.broadcast.emit('textupdate', JSON.stringify(data))

  })

  socket.on('disconnect', () => {

    debug('someone has disconnected from the socket')
    clientdata[sid] = undefined
    io.emit('disconnected', JSON.stringify({
      'id': shajs('sha256').update(sid).digest('hex')
    }))

  })

})

server.listen(3000)