add chat/duck
parent
3b4efdb94b
commit
f8fa7c4848
|
|
@ -1 +1,2 @@
|
||||||
export const address = 'http://localhost:9090'//'http://tbs093a.pythonanywhere.com'
|
export const address = 'http://localhost:9090'//'http://tbs093a.pythonanywhere.com'
|
||||||
|
export const webSocketAddress = 'ws://localhost:9090/ws/'
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
import chatReducer from './reducers'
|
||||||
|
export { default as chatTypes } from './types'
|
||||||
|
export { default as chatActions } from './actions'
|
||||||
|
|
||||||
|
export default chatReducer
|
||||||
|
|
@ -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))
|
||||||
|
}
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue