diff options
author | Maximilian Hils <git@maximilianhils.com> | 2016-06-23 23:38:19 -0700 |
---|---|---|
committer | Maximilian Hils <git@maximilianhils.com> | 2016-06-23 23:38:19 -0700 |
commit | 21c3480429108cbbd45f5ae3de7a36d15460cdc3 (patch) | |
tree | 43f7df8c382bee7b9e5feb831379541685cfb4c0 /web/src/js/ducks | |
parent | ed05d3f858fb6b7c93b2353c947d88b3aefcd1ae (diff) | |
parent | 57af1d00ddd16bd36609a8c6dbf61411f5cd504c (diff) | |
download | mitmproxy-21c3480429108cbbd45f5ae3de7a36d15460cdc3.tar.gz mitmproxy-21c3480429108cbbd45f5ae3de7a36d15460cdc3.tar.bz2 mitmproxy-21c3480429108cbbd45f5ae3de7a36d15460cdc3.zip |
Merge remote-tracking branch 'jason/websocket'
Diffstat (limited to 'web/src/js/ducks')
-rw-r--r-- | web/src/js/ducks/app.js | 27 | ||||
-rw-r--r-- | web/src/js/ducks/websocket.js | 117 |
2 files changed, 127 insertions, 17 deletions
diff --git a/web/src/js/ducks/app.js b/web/src/js/ducks/app.js new file mode 100644 index 00000000..f1dcb490 --- /dev/null +++ b/web/src/js/ducks/app.js @@ -0,0 +1,27 @@ +import { connect as wsConnect, disconnect as wsDisconnect } from './websocket' + +export const INIT = 'APP_INIT' + +const defaultState = {} + +export function reduce(state = defaultState, action) { + switch (action.type) { + + default: + return state + } +} + +export function init() { + return dispatch => { + dispatch(wsConnect()) + dispatch({ type: INIT }) + } +} + +export function destruct() { + return dispatch => { + dispatch(wsDisconnect()) + dispatch({ type: DESTRUCT }) + } +} diff --git a/web/src/js/ducks/websocket.js b/web/src/js/ducks/websocket.js index ebb39cf8..f38124c9 100644 --- a/web/src/js/ducks/websocket.js +++ b/web/src/js/ducks/websocket.js @@ -1,30 +1,113 @@ -const CONNECTED = 'WEBSOCKET_CONNECTED' -const DISCONNECTED = 'WEBSOCKET_DISCONNECTED' +import { ConnectionActions } from '../actions.js' +import { AppDispatcher } from '../dispatcher.js' +import * as eventLogActions from './eventLog' +import * as flowsActions from './flows' +import * as settingsActions from './settings' +export const SYM_SOCKET = Symbol('WEBSOCKET_SYM_SOCKET') -const defaultState = { - connected: false, - /* we may want to have an error message attribute here at some point */ -} -export default function reducer(state = defaultState, action) { +export const CONNECT = 'WEBSOCKET_CONNECT' +export const CONNECTED = 'WEBSOCKET_CONNECTED' +export const DISCONNECT = 'WEBSOCKET_DISCONNECT' +export const DISCONNECTED = 'WEBSOCKET_DISCONNECTED' +export const ERROR = 'WEBSOCKET_ERROR' +export const MESSAGE = 'WEBSOCKET_MESSAGE' + +/* we may want to have an error message attribute here at some point */ +const defaultState = { connected: false, socket: null } + +export default function reduce(state = defaultState, action) { switch (action.type) { + + case CONNECT: + return { ...state, [SYM_SOCKET]: action.socket } + case CONNECTED: - return { - connected: true - } + return { ...state, connected: true } + + case DISCONNECT: + return { ...state, connected: false } + case DISCONNECTED: - return { - connected: false - } + return { ...state, [SYM_SOCKET]: null, connected: false } + default: return state } } +export function connect() { + return dispatch => { + const socket = new WebSocket(location.origin.replace('http', 'ws') + '/updates') + + // @todo remove this + window.ws = socket -export function connected() { - return {type: CONNECTED} + socket.addEventListener('open', () => dispatch(onConnect())) + socket.addEventListener('close', () => dispatch(onDisconnect())) + socket.addEventListener('message', msg => dispatch(onMessage(msg))) + socket.addEventListener('error', error => dispatch(onError(error))) + + dispatch({ type: CONNECT, socket }) + + return socket + } +} + +export function disconnect() { + return (dispatch, getState) => { + getState().settings[SYM_SOCKET].close() + dispatch({ type: DISCONNECT }) + } } -export function disconnected() { - return {type: DISCONNECTED} + +export function onConnect() { + // workaround to make sure that our state is already available. + return dispatch => { + dispatch({ type: CONNECTED }) + dispatch(settingsActions.fetchSettings()) + dispatch(flowsActions.fetchFlows()).then(() => ConnectionActions.open()) + } +} + +export function onMessage(msg) { + return dispatch => { + const data = JSON.parse(msg.data) + + AppDispatcher.dispatchServerAction(data) + + switch (data.type) { + + case eventLogActions.UPDATE_LOG: + return dispatch(eventLogActions.updateLogEntries(data)) + + case flowsActions.UPDATE_FLOWS: + return dispatch(flowsActions.updateFlows(data)) + + case settingsActions.UPDATE_SETTINGS: + return dispatch(settingsActions.updateSettings(message)) + + default: + console.warn('unknown message', data) + } + + dispatch({ type: MESSAGE, msg }) + } +} + +export function onDisconnect() { + return dispatch => { + ConnectionActions.close() + dispatch(eventLogActions.addLogEntry('WebSocket connection closed.')) + dispatch({ type: DISCONNECTED }) + } +} + +export function onError(error) { + // @todo let event log subscribe WebSocketActions.ERROR + return dispatch => { + ConnectionActions.error() + dispatch(eventLogActions.addLogEntry('WebSocket connection error.')) + dispatch({ type: ERROR, error }) + } } |