-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender.js
149 lines (131 loc) · 4.62 KB
/
sender.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
139
140
141
142
143
144
145
146
147
148
149
const { createHash } = require('crypto');
module.exports = class {
state;
socket;
headers;
maxBufferLength = 65500;
payloadData = '';
bufferOffset = 2;
sendingBuffer;
constructor(socket, headers = null) {
this.socket = socket;
this.headers = headers;
}
setState(data) {
this.state = data;
}
getState() {
return this.state;
}
uintToBase62(n) {
if (n < 0) throw 'unsupported negative integer';
if (n > 0x7FFFFFFF) {
this.sendingBuffer.writeUInt32BE(n, 2);
} else {
// `~~` double bitwise operator
// The most practical way of utilizing the power of this operator is to use it as a replacement
// for Math.floor() function as double bitwise NOT performs the same operation a lot quicker.
// You can use it, to convert any floating point number to a integer without performance overkill
// that comes with Math.floor(). Additionally, when you care about minification of your code,
// you end up using 2 characters (2 tildes) instead of 12.
// http://rocha.la/JavaScript-bitwise-operators-in-practice
const big = ~~(n / 0x0100000000);
const low = (n % 0x0100000000);
this.sendingBuffer.writeUInt32BE(big, 2);
this.sendingBuffer.writeUInt32BE(low, 6);
}
}
formatBuffer() {
const payloadLength = Buffer.byteLength(this.payloadData);
let bufferStatus = 0b10000001;
let bufferLength = payloadLength;
let payloadIndexLength = payloadLength;
if(payloadLength <= 125) {
payloadIndexLength = payloadLength;
} else if(payloadLength <= this.maxBufferLength) {
this.bufferOffset += 2;
payloadIndexLength = 126;
} else if(payloadLength > this.maxBufferLength) {
bufferLength = this.maxBufferLength;
payloadIndexLength = 127;
this.bufferOffset += 8;
}
this.sendingBuffer = Buffer.alloc(this.bufferOffset + bufferLength);
this.sendingBuffer.writeUInt8(bufferStatus, 0);
this.sendingBuffer.writeUInt8(payloadIndexLength, 1);
if(payloadIndexLength === 126) {
this.sendingBuffer.writeUInt16BE(payloadLength, 2);
} else if(payloadIndexLength === 127) {
this.uintToBase62(payloadLength);
// this.sendingBuffer.writeUInt32BE(payloadLength, 6);
}
}
setHeader(headers) {
this.headers = headers;
}
socketKey() {
return this.headers['sec-websocket-key'];
}
acceptKey() {
let magicStr = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
let secWSA = this.socketKey() + magicStr;
var shasum = createHash('sha1')
shasum.update(secWSA);
return shasum.digest('base64');
}
handShakeHeader() {
let eol = "\r\n";
let headers = "HTTP/1.1 101 Switching Protocols" + eol
headers += "Connection: Upgrade" + eol;
headers += "Upgrade: websocket" + eol;
headers += `Sec-WebSocket-Accept: ${this.acceptKey()}${eol}${eol}`;
return headers;
}
handShake() {
this.writeOnSocket(this.handShakeHeader());
}
isPayLoadEmpty() {
return this.payloadData.length === 0 ? true : false;
}
writePayloadData() {
let writedByteCount;
if(!this.sendingBuffer) {
let tmpBufSize;
if(this.payloadData.length > this.maxBufferLength) {
tmpBufSize = this.maxBufferLength;
} else {
tmpBufSize = this.payloadData.length;
}
this.sendingBuffer = Buffer.alloc(tmpBufSize);
}
writedByteCount = this.sendingBuffer.write(this.payloadData, this.bufferOffset);
if(this.payloadData.length > writedByteCount) {
this.payloadData = this.payloadData.slice(writedByteCount);
} else {
this.payloadData = '';
}
// work from here
}
write(msg) {
this.payloadData = msg;
this.formatBuffer();
while(!this.isPayLoadEmpty()) {
this.writePayloadData();
this.writeOnSocket(this.sendingBuffer);
this.sendingBuffer = null;
this.bufferOffset = 0;
}
this.bufferOffset = 2;
}
send(msg) {
this.write(msg);
}
ping() {
// 0x89 0x05 0x48 0x65 0x6c 0x6c 0x6f
const sendingBuffer = Buffer.from([0x89, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f]);
this.writeOnSocket(sendingBuffer);
}
writeOnSocket(data) {
this.socket.write(data);
}
}