React -> upgrade websockets

feature/2_forms
TBS093A 2021-02-19 00:19:06 +01:00
parent 28492e2f2a
commit 4088161363
3 changed files with 44 additions and 21 deletions

View File

@ -4,7 +4,9 @@ import { useSelector, useDispatch } from 'react-redux'
import { userAuthSelector } from '../../../redux/slices/userAuthSlice'
import { modelCrudSelector } from '../../../redux/slices/modelCrudSlice'
import { renderWebsocketSelector } from '../../../redux/slices/renderWebsocketSlice'
import renderWebsocketAsyncThunk from '../../../redux/asyncThunks/renderWebsocketAsyncThunk'
import { connect, saveMessage, disconnect } from '../../../redux/slices/renderWebsocketSlice'
import modelCrudAsyncThunk from '../../../redux/asyncThunks/modelCrudAsyncThunk'
import FormGenerator from '../formGenerator'
@ -137,16 +139,20 @@ const RenderSingleImageForm = () => {
}
if ( web_socket === null && address === '' && room_uuid === '') {
dispatch( renderWebsocketAsyncThunk.fetchConnect( 'image' ) )
connect(
{
address: '/single/image/'
}
)
}
if ( address !== '' ) {
web_socket.onmessage = (event) => {
dispatch(
renderWebsocketAsyncThunk.fetchSaveMessage(
JSON.parse( event.data )
)
)
if ( address !== '' && web_socket !== null ) {
web_socket.onmessage = (event) => {
saveMessage(
{
message: JSON.parse( event.data )
}
)
}
}

View File

@ -1 +1,3 @@
export const GeneralAddress = 'http://localhost:9090'
const API = 'localhost:9090'
export const GeneralAddress = 'http://' + API
export const GeneralAddressWS = 'ws://' + API

View File

@ -1,6 +1,19 @@
import { createSlice } from '@reduxjs/toolkit'
import renderWebsocketAsyncThunk from '../asyncThunks/renderWebsocketAsyncThunk'
import GeneralAddressWS from '../asyncThunks/abstracts/abstractAddress'
const __uuidv4 = () => {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
/[xy]/g,
(c) => {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
}
)
}
const renderWebsocketSlice = createSlice(
{
name: 'render/async',
@ -11,32 +24,34 @@ const renderWebsocketSlice = createSlice(
messages: [],
connected: false
},
reducers: {},
extraReducers: {
[renderWebsocketAsyncThunk.fetchConnect.fulfilled.type]: (state, action) => {
state.web_socket = action.web_socket
state.address = action.address
state.room_uuid = action.room_uuid
reducers: {
connect(state, action) {
state.room_uuid = __uuidv4()
state.address = GeneralAddressWS + action.payload.address
state.web_socket = new WebSocket( state.address + state.room_uuid )
state.messages = []
state.connected = true
},
[renderWebsocketAsyncThunk.fetchSaveMessage.fulfilled.type]: (state, action) => {
saveMessage(state, action) {
state.messages = [
...state.messages,
action.message
action.payload.message
]
},
[renderWebsocketAsyncThunk.fetchDisconnect.fulfilled.type]: (state, action) => {
state.web_socket = action.web_socket
disconnect(state) {
state.web_socket = null
state.address = ''
state.room_uuid = ''
state.messages = []
state.connected = false
}
}
},
extraReducers: {}
}
)
export const renderWebsocketReducer = renderWebsocketSlice.reducer
export const { connect, saveMessage, disconnect } = renderWebsocketSlice.actions
export const renderWebsocketSelector = state => state.renderWebsocketReducer