-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsockets.js
138 lines (107 loc) · 3.47 KB
/
websockets.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { createNanoEvents } from "nanoevents"
export class Websocket {
constructor (url, options) {
this.url = url
this.options = options
this.emitter = createNanoEvents()
this.sendQueue = []
if (this.url.includes('wss:')) {
this.url = this.url.replace('wss:', 'https:')
}
if (this.url.includes('ws:')) {
this.url = this.url.replace('ws:', 'http:')
}
this.connect()
}
log(msg) {
if (this.options.logger) {this.options.logger.send(msg)}
}
async connect() {
// Undocumented API at time of writing.
var resp = await fetch(this.url, { headers: {'upgrade': 'websocket'} })
this.socket = resp.webSocket
this.socket.accept()
this.socket.addEventListener('message', (msg) => {
this.emitter.emit('rawmessage', msg)
var data = msg.data
try {
data = JSON.parse(data)
if (typeof data == 'string') {
// JSON convert returned a String, that means the original was a string.
// we want to return the original frame as we dont want to add extra quotes to the string.
data = msg.data
}
} catch (e) {
// ignore parser errors
}
this.emitter.emit('message', data)
})
this.socket.addEventListener('close', () => {
this.emitter.emit('close')
})
this.sendQueue.forEach(queued => {
this.socket.send(queued)
})
this.sendQueue = []
}
on(event, callback) {
return this.emitter.on(event, callback)
}
addEventListener(event, callback) {
if (event == 'message') {
event = 'rawmessage'
}
return this.emitter.on(event, callback)
}
send(data, options) {
var toSend = data
if (data.constructor == Object || data.constructor == Array) {
toSend = JSON.stringify(toSend)
}
if (!this.socket) {
this.sendQueue.push(toSend)
} else {
this.socket.send(toSend)
}
}
}
export class WebsocketResponse {
constructor () {
let pair = new WebSocketPair()
this.socket = pair[1]
this.client = pair[0]
// accept our end of the socket
this.socket.accept()
this.emitter = createNanoEvents()
this.session = {
history: [],
startTime: new Date(),
lastMessageTime: null,
}
this.socket.addEventListener('message', (msg) => {
var data = msg.data
try {
data = JSON.stringify(data)
if (typeof data == 'string') {
// dont JSON wrap data that is already a string.
data = msg.data
}
} catch (e) {
// couldn't parse incoming data
}
this.session.lastMessageTime = new Date()
this.emitter.emit('message', data)
})
this.socket.addEventListener('close', () => this.emitter.emit('close'))
}
on(event, callback) {
return this.emitter.on(event, callback)
}
send(data, options) {
var toSend = data
if (data.constructor == Object || data.constructor == Array) {
toSend = JSON.stringify(toSend)
}
this.socket.send(toSend)
}
}