diff --git a/src/stores/apiAddress.js b/src/stores/apiAddress.js index 3555e22..e1a8ee1 100644 --- a/src/stores/apiAddress.js +++ b/src/stores/apiAddress.js @@ -1 +1,2 @@ export const address = 'http://localhost:9090'//'http://tbs093a.pythonanywhere.com' +export const webSocketAddress = 'ws://localhost:9090/ws/' \ No newline at end of file diff --git a/src/stores/chat/duck/action.js b/src/stores/chat/duck/action.js new file mode 100644 index 0000000..c743fc6 --- /dev/null +++ b/src/stores/chat/duck/action.js @@ -0,0 +1,34 @@ +import types from './types' + +const wsConnect = host => ({ + type: types.WS_CONNECT, host +}); + +const wsConnecting = host => ({ + type: types.WS_CONNECTING, host +}); + +const wsConnected = host => ({ + type: types.WS_CONNECTED, host +}); + +const wsDisconnect = host => ({ + type: types.WS_DISCONNECT, host +}); + +const wsDisconnected = host => ({ + type: types.WS_DISCONNECTED, host +}); + +const wsSaveMessage = host => ({ + type: types.WS_SAVE_MESSAGE, host +}) + +export default { + wsConnect, + wsConnecting, + wsConnected, + wsDisconnect, + wsDisconnected, + wsSaveMessage +} \ No newline at end of file diff --git a/src/stores/chat/duck/index.js b/src/stores/chat/duck/index.js new file mode 100644 index 0000000..9aaeacf --- /dev/null +++ b/src/stores/chat/duck/index.js @@ -0,0 +1,5 @@ +import chatReducer from './reducers' +export { default as chatTypes } from './types' +export { default as chatActions } from './actions' + +export default chatReducer \ No newline at end of file diff --git a/src/stores/chat/duck/operations.js b/src/stores/chat/duck/operations.js new file mode 100644 index 0000000..a3b4327 --- /dev/null +++ b/src/stores/chat/duck/operations.js @@ -0,0 +1,29 @@ +import actions from './actions' +import { webSocketAddress } from './../../apiAddress' + +// export const chatConnect = ( lobby ) => +// async ( dispatch ) => { +// const host = webSocketAddress + '' + lobby; +// try { +// dispatch(actions.wsConnect(host)); +// } catch { +// console.log('chat connect error'); +// } +// } + +export const chatOnOpen = ( event ) => + async ( dispatch ) => { + console.log('websocket open', event.target.url) + dispatch(actions.wsConnected(event.target.url)) +} + +export const chatOnClose = () => + async ( dispatch ) => { + dispatch(actions.wsDisconnected()) +} + +export const chatOnMessage = ( event ) => + async ( dispatch ) => { + const payload = JSON.parse(event.data) + dispatch(actions.wsSaveMessage(payload)) +} \ No newline at end of file diff --git a/src/stores/chat/duck/reducers.js b/src/stores/chat/duck/reducers.js new file mode 100644 index 0000000..bf2e3db --- /dev/null +++ b/src/stores/chat/duck/reducers.js @@ -0,0 +1,17 @@ +import types from './types' + +const INITIAL_STATE = { + lobby: '', + socket: '', + messages: [] +} + +const chatReducer = ( state = INITIAL_STATE, action ) => { + switch (action.type) { + case types.WS_CONNECT: + return { + ...state, + socket: new WebSocket(action.host) + } + } +} \ No newline at end of file diff --git a/src/stores/chat/duck/types.js b/src/stores/chat/duck/types.js new file mode 100644 index 0000000..2fca436 --- /dev/null +++ b/src/stores/chat/duck/types.js @@ -0,0 +1,15 @@ +const WS_CONNECT = 'WS_CONNECT' +const WS_CONNECTING = 'WS_CONNECTING' +const WS_CONNECTED = 'WS_CONNECTED' +const WS_DISCONNECT = 'WS_DISCONNECT' +const WS_DISCONNECTED = 'WS_DISCONNECTED' +const WS_SAVE_MESSAGE = 'WS_SAVE_MESSAGE' + +export default { + WS_CONNECT, + WS_CONNECTING, + WS_CONNECTED, + WS_DISCONNECT, + WS_DISCONNECTED, + WS_SAVE_MESSAGE +} \ No newline at end of file