websocket客户端

web端,走websocket方式通信;
源码:https://github.com/mpusher/mpush-client-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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MPush WebSocket Client</title>
</head>
<body>

<script type="text/javascript">
(function (window) {
let socket, session = {}, ID_SEQ = 1;
let config = {listener: null, log: console};
let listener = {
onOpened: function (event) {
if (config.listener != null) {
config.listener.onOpened(event);
}
handshake();
},
onClosed: function (event) {
if (config.listener != null) {
config.listener.onClosed(event);
}
session = {};
ID_SEQ = 1;
socket = null;
},
onHandshake: function () {
session.handshakeOk = true;
if (config.listener != null) {
config.listener.onHandshake();
}
if (config.userId) {
bindUser(config.userId, config.tags);
}
},
onBindUser: function (success) {
if (config.listener != null) {
config.listener.onBindUser(success);
}
},
onReceivePush: function (message, messageId) {
if (config.listener != null) {
config.listener.onReceivePush(message, messageId);
}
},
onKickUser: function (userId, deviceId) {
if (config.listener != null) {
config.listener.onKickUser(userId, deviceId);
}
doClose(-1, "kick user");
}
};
const Command = {
HANDSHAKE: 2,
BIND: 5,
UNBIND: 6,
ERROR: 10,
OK: 11,
KICK: 13,
PUSH: 15,
ACK: 23,
UNKNOWN: -1
};
function Packet(cmd, body, sessionId) {
return {
cmd: cmd,
flags: 16,
sessionId: sessionId || ID_SEQ++,
body: body
}
}
function handshake() {
send(Packet(Command.HANDSHAKE, {
deviceId: config.deviceId,
osName: config.osName,
osVersion: config.osVersion,
clientVersion: config.clientVersion
})
);
}
function bindUser(userId, tags) {
if (userId && userId != session.userId) {
session.userId = userId;
session.tags = tags;
send(Packet(Command.BIND, {userId: userId, tags: tags}));
}
}
function ack(sessionId) {
send(Packet(Command.ACK, null, sessionId));
}
function send(message) {
if (!socket) {
return;
}
if (socket.readyState == WebSocket.OPEN) {
socket.send(JSON.stringify(message));
} else {
config.log.error("The socket is not open.");
}
}
function dispatch(packet) {
switch (packet.cmd) {
case Command.HANDSHAKE: {
config.log.debug(">>> handshake ok.");
listener.onHandshake();
break;
}
case Command.OK: {
if (packet.body.cmd == Command.BIND) {
config.log.debug(">>> bind user ok.");
listener.onBindUser(true);
}
break;
}
case Command.ERROR: {
if (packet.body.cmd == Command.BIND) {
config.log.debug(">>> bind user failure.");
listener.onBindUser(false);
}
break;
}
case Command.KICK: {
if (session.userId == packet.body.userId && config.deviceId == packet.body.deviceId) {
config.log.debug(">>> receive kick user.");
listener.onKickUser(packet.body.userId, packet.body.deviceId);
}
break;
}
case Command.PUSH: {
config.log.debug(">>> receive push, content=" + packet.body.content);
let sessionId;
if ((packet.flags & 8) != 0) {
ack(packet.sessionId);
} else {
sessionId = packet.sessionId
}
listener.onReceivePush(packet.body.content, sessionId);
break;
}
}
}
function onReceive(event) {
config.log.debug(">>> receive packet=" + event.data);
dispatch(JSON.parse(event.data))
}
function onOpen(event) {
config.log.info("Web Socket opened!");
listener.onOpened(event);
}
function onClose(event) {
config.log.info("Web Socket closed!");
listener.onClosed(event);
}
function onError(event) {
config.log.info("Web Socket receive, error");
doClose();
}
function doClose(code, reason) {
if (socket) socket.close();
config.log.info("try close web socket client, reason=" + reason);
}
function doConnect(cfg) {
config = copy(cfg);
socket = new WebSocket(config.url);
socket.onmessage = onReceive;
socket.onopen = onOpen;
socket.onclose = onClose;
socket.onerror = onError;
config.log.debug("try connect to web socket server, url=" + config.url);
}
function copy(cfg) {
for (let p in cfg) {
if (cfg.hasOwnProperty(p)) {
config[p] = cfg[p];
}
}
return config;
}
window.mpush = {
connect: doConnect,
close: doClose,
bindUser: bindUser
}
})(window);
function $(id) {
return document.getElementById(id);
}
let log = {
log: function () {
$("responseText").value += (Array.prototype.join.call(arguments, "") + "\r\n");
}
};
log.debug = log.info = log.warn = log.error = log.log;
function connect() {
mpush.connect({
url: $("url").value,
userId: $("userId").value,
deviceId: "test-1001",
osName: "web " + navigator.userAgent,
osVersion: "55.2",
clientVersion: "1.0",
log: log
});
}
function bind() {
mpush.bindUser($("userId").value)
}
</script>
<form onsubmit="return false;">
<label> Server Url:
<input type="text" id="url" value="ws://127.0.0.1:8080/">
</label>
<input type="button" value="Connect" onclick="connect()">
<br>
<label> Bind User:
<input type="text" id="userId" value="user-0">
</label>
<input type="button" value="bind" onclick="bind()">
<h3><label for="responseText">Output</label></h3>
<textarea id="responseText" style="width:100%;height:500px;"></textarea>
</form>

</body>
</html>
------ 本文结束 感谢您的阅读 ------