Skip to content

Instantly share code, notes, and snippets.

@natdm
Created July 14, 2016 16:40
Show Gist options
  • Save natdm/411409ef55c4ba38775a5d6bb1a37fbb to your computer and use it in GitHub Desktop.
Save natdm/411409ef55c4ba38775a5d6bb1a37fbb to your computer and use it in GitHub Desktop.
import { AppState } from 'react-native';
import { setError, clearError } from '../reducers/error';
import { updateEventStatus } from '../reducers/event_details';
import { setAlert } from '../reducers/alert';
import { ws_url } from '../api/urls';
import handleOnMessage, { UPDATE_EVENT_DETAILS, SET_EVENT_STATUS } from './handleOnMessage';
import * as r from '../scenes/scene_names/routes';
import store from '../store/store';
//Socket connection readyStates are integers that correlate to these values
export const readyStates = {
0: "CONNECTING"
, 1: "OPEN"
, 2: "CLOSING"
, 3: "CLOSED"
}
class WSConn {
constructor() {
this.conn = new WebSocket(ws_url);
this.navigator = null;
}
setNavigator(nav) {
this.navigator = nav;
}
reconnect() {
this.conn = new WebSocket(ws_url);
this.connect();
}
checkReadyState() {
let state = store.getState();
setTimeout(() => {
if (this.conn.readyState == 1) {
if (typeof state.event_details.event_details != 'undefined') {
this.setSocketedEventInfo(state.event_details.event_details.event.event_id);
}
store.dispatch(clearError());
} else {
store.dispatch(setError('fatal', `Socket readyState should be ${readyStates[1]} but it's ${readyStates[this.conn.readyState]}`))
}
}, 1000);
}
connect() {
this.checkReadyState();
this.handleSocketConnections();
this.conn.onclose = e => store.dispatch(
setError("fatal", `Socket onclose: ${e.code} -- ${e.reason}`)
);
this.conn.onerror = e => store.dispatch(
setError("fatal", `Socket onerror: ${e.message}`)
);
}
setSocketedEventInfo(event_id) {
const msg = {
type: UPDATE_EVENT_DETAILS
, payload: { event_id }
}
this.conn.send(JSON.stringify(msg));
}
createBid(bid, cb) {
const new_bid = {
type: 'BID'
, payload: bid
};
// Send this to the server socket
this.conn.send(JSON.stringify(new_bid));
//Returning the callback so the front-end knows to flip the card back over.
return cb()
}
setAppStateHandler() {
//If the phone falls asleep and the new change is to active, it means the
// phone woke up and should re-establish websocket connection.
AppState.addEventListener('change', cstate => {
if (cstate === 'active') {
this.reconnect();
}
})
}
handleSocketConnections() {
this.setAppStateHandler();
this.conn.onmessage = e => {
const state = store.getState();
const msg = JSON.parse(e.data);
const { type, payload, event_id } = msg;
const { event } = state.event_details.event_details;
if (type == SET_EVENT_STATUS && payload == "CLOSED" && event_id == event.event_id) {
this.navigator.push(r.EVENTENDED)
// store.dispatch(setAlert({
// message:"Event is closed, click to navigate to checkout."
// , scene: null
// }))
store.dispatch(updateEventStatus(payload));
} else {
handleOnMessage(msg, state)
}
}
}
}
const websocket = new WSConn()
export default websocket
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment