summaryrefslogtreecommitdiff
path: root/public/js/lib/ws.js
blob: 37a824856ad8357e818865dac8f4acd2b8f2a047 (plain)
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
var ws = function(){
  var ws = {}
  var socket, socketIsReady
	ws.connect = function () {
    if (this.socket) return;
    var socketPath = window.location.origin + '/' + posthang.room.subdomain

    ws.socket = socket = io(socketPath)
    // this.socket.on('connect', function(){ console.log(new Date(), "connected")})
    socket.on('ready', ws.ready)
    socket.on('error', ws.error)
    socket.on('connect', ws.connected)
    socket.on('reconnect', ws.reconnected)
    socket.on('disconnect', ws.disconnected)
  }
  
  ws.ready = function (obj) {
    console.log(new Date(), "ready")
    
    // presumably we might have reconnected?
    if (socketIsReady) {
      this.chatView.fetchAndDedupe()
    }
    else {
      socketIsReady = true
      this.checkIfLoaded()
    }
  }

  ws.error = function (a,b,c){
    console.log(new Date(), "error", a, b, c)
  }
  ws.connected = function (){
    console.log(new Date(), "connected")
  }
  ws.reconnected = function (){
    console.log(new Date(), "reconnected")
  }
  ws.disconnected = function (){
    console.log(new Date(), "disconnected")
    // this.chatView.appendInfo({ content: "Disconnected." })
  }
}