Commit 8063656c by zeven

修复微信刷脸支付导致socket掉线问题

parent 9d676d50
android @ ea22824f
Subproject commit a68acacc0bd5b01ab34362b4ef09bbab72ded3d5
Subproject commit ea22824f4000883940b000f1e1854893e7463f1e
......@@ -54,6 +54,7 @@ class App extends React.Component {
};
async componentWillMount() {
// await AsyncStorage.clear(); // 清除数据
window.dispatch = this.props.dispatch;
const HOST = await AsyncStorage.getItem('HOST');
axios.defaults.baseURL = HOST || host; // 配置接口地址
......
import AsyncStorage from '@react-native-community/async-storage';
import * as api from '../services/admin';
import axios from 'axios';
import * as api from '../services/admin';
import SocketIO from '../utils/socketio';
export default {
namespace: 'admin',
......@@ -14,9 +15,18 @@ export default {
state.auth = auth;
window.auth = auth;
if (window.socket) {
window.socket.io.opts.query = {
token: auth.token,
};
if (window.socket.io) {
window.socket.io.opts.query = {
token: auth.token,
};
} else {
// 使用原生SocketIO
window.socket.disconnect();
window.socket = new SocketIO(
`${axios.defaults.baseURL}/socket.io/?token=${window.auth.token}`,
);
window.socket.connect();
}
}
AsyncStorage.setItem('auth', JSON.stringify(auth));
if (login) {
......
......@@ -138,7 +138,7 @@ const CounterMixins = ComposeComponent => {
window.socket.emit(
'counterGoods',
sid,
goods,
JSON.stringify(goods),
totalPrice,
totalCount,
payType,
......
import io from 'socket.io-client';
import axios from 'axios';
import SocketIO from '../utils/socketio';
import Restart from './Restart';
window.restart = Restart.restartApp;
......@@ -7,9 +7,15 @@ window.restart = Restart.restartApp;
export async function counterInterval(dispatch, expiresIn, reload) {
window.socket =
window.socket ||
io(axios.defaults.baseURL, {
query: {token: window.auth.token},
new SocketIO(axios.defaults.baseURL, {
query: `token=${window.auth.token}`,
});
window.socket.connect();
// window.socket =
// window.socket ||
// io(axios.defaults.baseURL, {
// query: {token: window.auth.token},
// });
window.authInterval = window.setInterval(() => {
dispatch({type: 'admin/auth'});
}, 8 * 60 * 60 * 1000); // 每8小时更新一次
......
import {DeviceEventEmitter, NativeModules, Platform} from 'react-native';
import {debounce} from 'throttle-debounce';
let SocketIO = NativeModules.SocketIO;
class Socket {
constructor(host, config) {
if (typeof host === 'undefined') {
throw 'Hello there! Could you please give socket a host, please.';
}
if (typeof config === 'undefined') {
config = {};
}
this.sockets = SocketIO;
this.isConnected = false;
this.handlers = {};
this.onAnyHandler = null;
this.deviceEventSubscription = DeviceEventEmitter.addListener(
'socketEvent',
this._handleEvent.bind(this),
);
// Set default handlers
this.defaultHandlers = {
connect: e => {
if (e && e.sid) {
this.id = e.sid;
}
this.isConnected = true;
},
disconnect: () => {
this.isConnected = false;
},
};
// Set initial configuration
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);
}
if (this.handlers.hasOwnProperty(event.name)) {
const res = event.hasOwnProperty('items') ? event.items[0] : {};
this.handlers[event.name](res);
}
if (this.onAnyHandler) {
this.onAnyHandler(event);
}
}
hasListeners(eventName) {
return this.handlers.hasOwnProperty(eventName);
}
getId() {
return this.sockets.getId();
}
connect() {
this.sockets.connect();
}
on(event, handler) {
this.handlers[event] = handler;
if (Platform.OS === 'android') {
this.sockets.on(event);
}
}
onAny(handler) {
this.onAnyHandler = handler;
}
emit(event, ...data) {
this.sockets.emit(event, data);
}
joinNamespace(namespace) {
this.sockets.joinNamespace(namespace);
}
leaveNamespace() {
this.sockets.leaveNamespace();
}
disconnect() {
this.sockets.disconnect();
}
reconnect() {
this.sockets.reconnect();
}
}
export default Socket;
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