123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469 |
- const CC_WECHATGAME = false
- function Emitter (obj) {
- if (obj) return mixin(obj)
- };
- function mixin (obj) {
- for (var key in Emitter.prototype) {
- obj[key] = Emitter.prototype[key]
- }
- return obj
- }
- Emitter.prototype.on =
- Emitter.prototype.addEventListener = function (event, fn) {
- this._callbacks = this._callbacks || {};
- (this._callbacks['$' + event] = this._callbacks['$' + event] || [])
- .push(fn)
- return this
- }
- Emitter.prototype.once = function (event, fn) {
- function on () {
- this.off(event, on)
- fn.apply(this, arguments)
- }
- on.fn = fn
- this.on(event, on)
- return this
- }
- Emitter.prototype.off =
- Emitter.prototype.removeListener =
- Emitter.prototype.removeAllListeners =
- Emitter.prototype.removeEventListener = function (event, fn) {
- this._callbacks = this._callbacks || {}
-
- if (arguments.length === 0) {
- this._callbacks = {}
- return this
- }
-
- var callbacks = this._callbacks['$' + event]
- if (!callbacks) return this
-
- if (arguments.length === 1) {
- delete this._callbacks['$' + event]
- return this
- }
-
- var cb
- for (var i = 0; i < callbacks.length; i++) {
- cb = callbacks[i]
- if (cb === fn || cb.fn === fn) {
- callbacks.splice(i, 1)
- break
- }
- }
- return this
- }
- Emitter.prototype.emit = function (event) {
- this._callbacks = this._callbacks || {}
- var args = [].slice.call(arguments, 1)
- var callbacks = this._callbacks['$' + event]
- if (callbacks) {
- callbacks = callbacks.slice(0)
- for (var i = 0, len = callbacks.length; i < len; ++i) {
- callbacks[i].apply(this, args)
- }
- }
- return this
- }
- Emitter.prototype.listeners = function (event) {
- this._callbacks = this._callbacks || {}
- return this._callbacks['$' + event] || []
- }
- Emitter.prototype.hasListeners = function (event) {
- return !!this.listeners(event).length
- }
- function WsManager (url, opts) {
- if (!(this instanceof WsManager)) {
- return new WsManager(url, opts)
- }
- if (url && (typeof url === 'object')) {
- opts = url
- url = undefined
- }
- Emitter(this)
- opts = opts || {}
- opts.path = opts.path || '/'
- this.opts = opts
- this.url = url
- this.lastPing = null
- this.socketCache = []
- this.socketMaxCache = 5
- this.readyState = 'closed'
- this.binaryType = opts.binaryType || 'blob'
- this._reconnectTimes = 0
- this._reconnectionDelay = opts.reconnectionDelay || 1000
- this.reconnection(opts.reconnection !== false)
- this.reconnectionAttempts(opts.reconnectionAttempts || Infinity)
- this.timeout(opts.timeout === null ? 20000 : opts.timeout)
- this.logStyle = 'color:blue; font-size:16px;font-weight:bold;'
- this.keepAliveInterval = 15000
- this.keepAliveTimeout = null
- this.keepAliveContent = opts.keepAliveContent || 1
- this.autoConnect = opts.autoConnect !== false
- if (this.autoConnect) {
- this.connect()
- }
- }
- WsManager.prototype.connect = function (fn) {
- if (~this.readyState.indexOf('open')) {
- return this
- }
- this.readyState = 'opening'
- let _socket = CC_WECHATGAME ? this.openWxConnect() : this.openH5Connect()
- this.socketCache.push(_socket)
- this.socket = _socket
- }
- WsManager.prototype.reconnect = function () {
- clearInterval(this.keepAliveTimeout)
- if (this.readyState === 'open' || this.readyState === 'opening') {
- } else {
- if (this._reconnectTimes < this._reconnectionAttempts) {
- if (this.socket) {
- this.socket.close()
- }
- this._reconnectTimes += 1
- this.readyState = 'reconnecting'
-
- setTimeout(() => {
- this.socket = CC_WECHATGAME ? this.openWxConnect() : this.openH5Connect()
- }, this._reconnectionDelay)
- } else {
-
- if (this.socket) {
- this.socket.close()
- }
- this._reconnectTimes = 1
- this.readyState = 'reconnecting'
- setTimeout(() => {
- this.socket = CC_WECHATGAME ? this.openWxConnect() : this.openH5Connect()
- }, 30000)
- }
- }
- }
- WsManager.prototype.destroy = function () {
- this._reconnection = false
- this.socket.close()
- }
- WsManager.prototype.keepAlive = function () {
- this.keepAliveTimeout = setInterval(() => {
- if (this.readyState === 'open') {
-
-
-
-
-
-
- this.send(this.keepAliveContent)
- } else if (this.readyState === 'closed') {
- this.reconnect()
- }
- }, this.keepAliveInterval)
- }
- WsManager.prototype.openWxConnect = function () {
- let _header = {}
- let _socket = wx.connectSocket({
- url: this.url,
- header: _header,
- success: function (ret) {}
- })
- _socket.onOpen((res) => {
- this.readyState = 'open'
- this.emit('open', res)
-
-
- this._reconnectTimes = 1
-
- this.keepAlive()
- })
- _socket.onClose((res) => {
-
- clearInterval(this.keepAliveTimeout)
- this.readyState = 'closed'
-
- if (this._reconnection) {
- this.reconnect()
- }
- this.emit('close', res)
- })
- _socket.onMessage((res) => {
- if (res.data !== 1) {
- this.emit('message', res.data)
- }
-
- })
- _socket.onError((res) => {
-
- clearInterval(this.keepAliveTimeout)
- if (this._reconnection) {
- if (this.readyState !== 'reconnecting') {
- this.readyState = 'error'
- this.reconnect()
- }
- } else {
- _socket.close()
- }
- this.emit('error', res.errMsg)
- })
- return _socket
- }
- WsManager.prototype.openH5Connect = function () {
- let _socket = new WebSocket(this.url)
-
- _socket.onopen = (event) => {
- this.readyState = 'open'
- this.emit('open', event)
-
-
- this._reconnectTimes = 1
-
- this.keepAlive()
- }
- _socket.onclose = (event) => {
- clearInterval(this.keepAliveTimeout)
- this.readyState = 'closed'
-
- let reason = event.reason
-
- console.log(`%c [Socket连接被关闭: ${reason}]`, this.logStyle)
-
- if (this._reconnection) {
- this.reconnect()
- }
- this.emit('close', event)
- }
- _socket.onmessage = (event) => {
- if (event.data !== 1) {
- this.emit('message', event.data)
- }
-
- }
- _socket.onerror = (event) => {
-
- clearInterval(this.keepAliveTimeout)
- if (this._reconnection) {
- if (this.readyState !== 'reconnecting') {
- this.readyState = 'error'
- this.reconnect()
- }
- } else {
- _socket.close()
- }
- this.emit(event)
- }
- return _socket
- }
- WsManager.prototype.send = function (data) {
-
- if (CC_WECHATGAME) {
- let buffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength)
- this.socket.send({
- data: buffer,
- success: function (res) {
-
- },
- fail: function (res) {
-
- },
- complete: function (res) {
-
- }
- })
- } else {
- this.socket.binaryType = this.binaryType
- this.socket.send(data)
- }
- }
- WsManager.prototype.timeout = function (v) {
- if (!arguments.length) {
- return this._timeout
- }
- this._timeout = v
- return this
- }
- WsManager.prototype.reconnection = function (v) {
- if (!arguments.length) {
- return this._reconnection
- }
- this._reconnection = !!v
- return this
- }
- WsManager.prototype.reconnectionAttempts = function (v) {
- if (!arguments.length) {
- return this._reconnectionAttempts
- }
- this._reconnectionAttempts = v
- return this
- }
- export default WsManager
|