Commit 9a9a22da by zeven

修复刷新token创建socket实例监听事件问题

parent 2c5595bd
android @ a3cbda86
Subproject commit ea22824f4000883940b000f1e1854893e7463f1e
Subproject commit a3cbda867ae325953176a19a5ed9da46db9d686e
......@@ -21,11 +21,9 @@ export default {
};
} else {
// 使用原生SocketIO
window.socket.disconnect();
window.socket = new SocketIO(axios.defaults.baseURL, {
window.socket = window.socket.initialize({
query: `token=${window.auth.token}`,
});
window.socket.connect();
}
}
AsyncStorage.setItem('auth', JSON.stringify(auth));
......
import {DeviceEventEmitter, NativeModules, Platform} from 'react-native';
import {debounce} from 'throttle-debounce';
let SocketIO = NativeModules.SocketIO;
......@@ -17,10 +16,7 @@ class Socket {
this.handlers = {};
this.onAnyHandler = null;
this.deviceEventSubscription = DeviceEventEmitter.addListener(
'socketEvent',
this._handleEvent.bind(this),
);
DeviceEventEmitter.addListener('socketEvent', this._handleEvent.bind(this));
// Set default handlers
this.defaultHandlers = {
......@@ -37,15 +33,11 @@ class Socket {
};
// Set initial configuration
this.host = host;
this.config = config;
this.sockets.initialize(host, config);
}
debounceHandler = debounce(1000, true, event => {
console.warn(event.name);
const res = event.hasOwnProperty('items') ? event.items[0] : {};
this.handlers[event.name](res);
});
_handleEvent(event) {
if (this.defaultHandlers.hasOwnProperty(event.name)) {
this.defaultHandlers[event.name](event);
......@@ -63,10 +55,27 @@ class Socket {
return this.handlers.hasOwnProperty(eventName);
}
initialize(config) {
config = {...this.config, ...config};
this.disconnect();
DeviceEventEmitter.removeAllListeners('socketEvent');
const socket = new Socket(this.host, config);
Object.keys(this.handlers).map(event => {
const handler = this.handlers[event];
socket.on(event, handler);
});
socket.connect();
return socket;
}
getId() {
return this.sockets.getId();
}
updateQuery(query) {
return this.sockets.updateQuery(query);
}
connect() {
this.sockets.connect();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment