websocket客户端 | 分类于 源码学习 , MPush , 源码分析 , SDK , mpush-client-js | 评论数: | 阅读次数: web端,走websocket方式通信;源码:https://github.com/mpusher/mpush-client-js 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225<!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> ------ 本文结束 感谢您的阅读 ------