|
| 1 | +import io from 'socket.io-client'; |
| 2 | +// import queryString from 'query-string'; |
| 3 | + |
| 4 | +// const parsed = queryString.parse(window.location.search); |
| 5 | + |
| 6 | +class Socket { |
| 7 | + constructor(ua = 'control', disable = false) { |
| 8 | + this.uid = this.random(); |
| 9 | + // this.group = parsed.group || 'prod'; |
| 10 | + this.disable = disable; |
| 11 | + this.socket = io('http://47.102.198.102:8080/', { |
| 12 | + transports: ['websocket'], |
| 13 | + query: { |
| 14 | + ua, |
| 15 | + group: 'prod', |
| 16 | + }, |
| 17 | + }); |
| 18 | + this.isSpeaker = false; |
| 19 | + this.speakerHandlers = []; |
| 20 | + |
| 21 | + this.socket.on('room', ({ speaker }) => { |
| 22 | + this.isSpeaker = speaker === this.socket.id; |
| 23 | + this.speakerHandlers.forEach((cb) => { |
| 24 | + if (typeof cb === 'function') { |
| 25 | + cb(this.isSpeaker); |
| 26 | + } |
| 27 | + }); |
| 28 | + }); |
| 29 | + } |
| 30 | + |
| 31 | + random = () => Date.now() + Math.random(); |
| 32 | + |
| 33 | + registerSpeakerHandler = (callback) => { |
| 34 | + this.unregisterSpeakerHandler(callback); |
| 35 | + this.speakerHandlers.push(callback); |
| 36 | + }; |
| 37 | + |
| 38 | + unregisterSpeakerHandler = (callback) => { |
| 39 | + let i = -1; |
| 40 | + this.speakerHandlers.some((cb, index) => { |
| 41 | + if (cb === callback) { |
| 42 | + i = index; |
| 43 | + return true; |
| 44 | + } |
| 45 | + return false; |
| 46 | + }); |
| 47 | + if (i >= 0) { |
| 48 | + this.speakerHandlers.splice(i, 1); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + dispatch = (data) => { |
| 53 | + // if (!this.isSpeaker && !data.speaker) { |
| 54 | + // return Promise.resolve(); |
| 55 | + // } |
| 56 | + |
| 57 | + data.uid = this.uid; |
| 58 | + data.timestamp = this.random(); |
| 59 | + this.socket.emit('dispatch', data); |
| 60 | + if (data.speaker) { |
| 61 | + return new Promise((resolve, reject) => { |
| 62 | + this.registerSpeakerHandler((r) => { |
| 63 | + if (r) { |
| 64 | + resolve(); |
| 65 | + } else { |
| 66 | + reject(); |
| 67 | + } |
| 68 | + }); |
| 69 | + }); |
| 70 | + } |
| 71 | + return new Promise((resolve) => { |
| 72 | + const callback = (res) => { |
| 73 | + if (this.uid === res.uid && res.timestamp === data.timestamp) { |
| 74 | + this.socket.off('dispatch', callback); |
| 75 | + resolve(); |
| 76 | + } |
| 77 | + }; |
| 78 | + this.socket.on('dispatch', callback); |
| 79 | + }); |
| 80 | + }; |
| 81 | + |
| 82 | + sent = (data, force) => { |
| 83 | + if (!this.isSpeaker && !force) { |
| 84 | + return; |
| 85 | + } |
| 86 | + data.uid = this.uid; |
| 87 | + data.group = this.group; |
| 88 | + this.socket.emit('msg', data); |
| 89 | + }; |
| 90 | + |
| 91 | + on = (name, call) => { |
| 92 | + if (!this.disable) { |
| 93 | + this.socket.on(name, call); |
| 94 | + } |
| 95 | + }; |
| 96 | + |
| 97 | + close = () => { |
| 98 | + this.socket.close(); |
| 99 | + }; |
| 100 | + |
| 101 | + send = (data) => { |
| 102 | + this.dispatch({ |
| 103 | + source: 'control', |
| 104 | + targets: ['control'], |
| 105 | + type: 'dispatch', |
| 106 | + payload: data, |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +const socket = new Socket('control', window.location.pathname === '/console'); |
| 112 | +export const MsgSocket = Socket; |
| 113 | + |
| 114 | +export default socket; |
0 commit comments