Commit 9a9a22da by zeven

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

parent 2c5595bd
android @ a3cbda86
Subproject commit ea22824f4000883940b000f1e1854893e7463f1e Subproject commit a3cbda867ae325953176a19a5ed9da46db9d686e
...@@ -21,11 +21,9 @@ export default { ...@@ -21,11 +21,9 @@ export default {
}; };
} else { } else {
// 使用原生SocketIO // 使用原生SocketIO
window.socket.disconnect(); window.socket = window.socket.initialize({
window.socket = new SocketIO(axios.defaults.baseURL, {
query: `token=${window.auth.token}`, query: `token=${window.auth.token}`,
}); });
window.socket.connect();
} }
} }
AsyncStorage.setItem('auth', JSON.stringify(auth)); AsyncStorage.setItem('auth', JSON.stringify(auth));
......
import {DeviceEventEmitter, NativeModules, Platform} from 'react-native'; import {DeviceEventEmitter, NativeModules, Platform} from 'react-native';
import {debounce} from 'throttle-debounce';
let SocketIO = NativeModules.SocketIO; let SocketIO = NativeModules.SocketIO;
...@@ -17,10 +16,7 @@ class Socket { ...@@ -17,10 +16,7 @@ class Socket {
this.handlers = {}; this.handlers = {};
this.onAnyHandler = null; this.onAnyHandler = null;
this.deviceEventSubscription = DeviceEventEmitter.addListener( DeviceEventEmitter.addListener('socketEvent', this._handleEvent.bind(this));
'socketEvent',
this._handleEvent.bind(this),
);
// Set default handlers // Set default handlers
this.defaultHandlers = { this.defaultHandlers = {
...@@ -37,15 +33,11 @@ class Socket { ...@@ -37,15 +33,11 @@ class Socket {
}; };
// Set initial configuration // Set initial configuration
this.host = host;
this.config = config;
this.sockets.initialize(host, 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) { _handleEvent(event) {
if (this.defaultHandlers.hasOwnProperty(event.name)) { if (this.defaultHandlers.hasOwnProperty(event.name)) {
this.defaultHandlers[event.name](event); this.defaultHandlers[event.name](event);
...@@ -63,10 +55,27 @@ class Socket { ...@@ -63,10 +55,27 @@ class Socket {
return this.handlers.hasOwnProperty(eventName); 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() { getId() {
return this.sockets.getId(); return this.sockets.getId();
} }
updateQuery(query) {
return this.sockets.updateQuery(query);
}
connect() { connect() {
this.sockets.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