mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 10:34:17 +00:00
added events handlers to basics events (" disconnect, connect... etc ")
This commit is contained in:
parent
0f2a4ac384
commit
1ec9c2c822
@ -2,6 +2,7 @@ import io from 'socket.io-client'
|
|||||||
import { verbosity } from 'core/libs/verbosity'
|
import { verbosity } from 'core/libs/verbosity'
|
||||||
import { connect } from 'umi'
|
import { connect } from 'umi'
|
||||||
import settings from 'core/libs/settings'
|
import settings from 'core/libs/settings'
|
||||||
|
import { notify } from 'core/libs/appInterface'
|
||||||
|
|
||||||
const maxDeep_Attemp = Number(2)
|
const maxDeep_Attemp = Number(2)
|
||||||
export class SocketConnection{
|
export class SocketConnection{
|
||||||
@ -22,7 +23,7 @@ export class SocketConnection{
|
|||||||
this.props = props
|
this.props = props
|
||||||
this.opts = {
|
this.opts = {
|
||||||
reconnection: true,
|
reconnection: true,
|
||||||
reconnectionAttempts: Infinity,
|
reconnectionAttempts: maxDeep_Attemp,
|
||||||
reconnectionDelay: 1000,
|
reconnectionDelay: 1000,
|
||||||
reconnectionDelayMax: 5000,
|
reconnectionDelayMax: 5000,
|
||||||
randomizationFactor: 0.5,
|
randomizationFactor: 0.5,
|
||||||
@ -61,6 +62,7 @@ export class SocketConnection{
|
|||||||
}
|
}
|
||||||
this.ioConn = io(this.state.address, this.opts)
|
this.ioConn = io(this.state.address, this.opts)
|
||||||
this.conn.open()
|
this.conn.open()
|
||||||
|
|
||||||
this.ioConn.on("connect_error", () => {
|
this.ioConn.on("connect_error", () => {
|
||||||
if (this.state.connAttemps >= maxDeep_Attemp) {
|
if (this.state.connAttemps >= maxDeep_Attemp) {
|
||||||
verbosity(['Maximun nº of attemps reached => max', maxDeep_Attemp + 1])
|
verbosity(['Maximun nº of attemps reached => max', maxDeep_Attemp + 1])
|
||||||
@ -69,7 +71,20 @@ export class SocketConnection{
|
|||||||
}
|
}
|
||||||
verbosity([`Strike [${this.state.connAttemps + 1}] / ${maxDeep_Attemp + 1} !`])
|
verbosity([`Strike [${this.state.connAttemps + 1}] / ${maxDeep_Attemp + 1} !`])
|
||||||
this.state.connAttemps = this.state.connAttemps + 1
|
this.state.connAttemps = this.state.connAttemps + 1
|
||||||
|
})
|
||||||
|
|
||||||
|
this.ioConn.on('disconnect', () => {
|
||||||
|
verbosity("Connection disconnect!")
|
||||||
|
})
|
||||||
|
this.ioConn.on('reconnect', (attemptNumber:number) => {
|
||||||
|
verbosity(["Connection reconected with (", attemptNumber , ") tries"])
|
||||||
|
})
|
||||||
|
this.ioConn.on('connect', () => {
|
||||||
|
notify.success("You are now online")
|
||||||
|
verbosity("Successfully connect")
|
||||||
|
})
|
||||||
|
this.ioConn.on('close', () => {
|
||||||
|
verbosity("Connection closed!")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,7 +96,6 @@ export class SocketConnection{
|
|||||||
this.ioConn.disconnect()
|
this.ioConn.disconnect()
|
||||||
},
|
},
|
||||||
close: () => {
|
close: () => {
|
||||||
verbosity("Connection closed!")
|
|
||||||
this.ioConn.close()
|
this.ioConn.close()
|
||||||
},
|
},
|
||||||
destroy: () => {
|
destroy: () => {
|
||||||
|
@ -16,6 +16,7 @@ export default {
|
|||||||
state: {
|
state: {
|
||||||
env_proccess: process.env,
|
env_proccess: process.env,
|
||||||
socket_conn: null,
|
socket_conn: null,
|
||||||
|
socket_opt: null,
|
||||||
server_key: keys.server_key,
|
server_key: keys.server_key,
|
||||||
resolvers: null,
|
resolvers: null,
|
||||||
|
|
||||||
@ -117,11 +118,10 @@ export default {
|
|||||||
const { user_id, access_token } = payload.authFrame
|
const { user_id, access_token } = payload.authFrame
|
||||||
yield put({ type: 'handleLogin', payload: { user_id, access_token, user_data: payload.dataFrame } })
|
yield put({ type: 'handleLogin', payload: { user_id, access_token, user_data: payload.dataFrame } })
|
||||||
},
|
},
|
||||||
*initializeSocket({payload}, {select}){
|
*initializeSocket({payload}, {select, put}){
|
||||||
if(!payload) return false
|
if(!payload) return false
|
||||||
|
const { type, address } = payload
|
||||||
new SocketConnection(payload)
|
yield put({ type: "handleSocket", payload: new SocketConnection(payload) })
|
||||||
|
|
||||||
},
|
},
|
||||||
*initializePlugins({ payload }, { select }){
|
*initializePlugins({ payload }, { select }){
|
||||||
const extended = yield select(state => state.extended)
|
const extended = yield select(state => state.extended)
|
||||||
@ -260,6 +260,10 @@ export default {
|
|||||||
handleUpdateResolvers(state, { payload }) {
|
handleUpdateResolvers(state, { payload }) {
|
||||||
state.resolvers = payload
|
state.resolvers = payload
|
||||||
},
|
},
|
||||||
|
handleSocket(state, { payload }) {
|
||||||
|
state.socket_conn = payload
|
||||||
|
state.socket_opt = payload.opts
|
||||||
|
},
|
||||||
handleUpdateAuthFrames(state, { payload }) {
|
handleUpdateAuthFrames(state, { payload }) {
|
||||||
state.session_authframe = payload
|
state.session_authframe = payload
|
||||||
state.session_token = payload.session_token,
|
state.session_token = payload.session_token,
|
||||||
|
@ -7,13 +7,12 @@ import * as antd from 'antd'
|
|||||||
import { objectToArray } from 'core'
|
import { objectToArray } from 'core'
|
||||||
import settings from 'core/libs/settings'
|
import settings from 'core/libs/settings'
|
||||||
|
|
||||||
const defaultSocketAddress = "85.251.59.39"
|
const defaultSocketAddress = "localhost:7000"
|
||||||
|
|
||||||
@connect(({ app }) => ({ app }))
|
@connect(({ app }) => ({ app }))
|
||||||
export default class SocketDebug extends React.Component{
|
export default class SocketDebug extends React.PureComponent{
|
||||||
state = {
|
state = {
|
||||||
resolvers: objectToArray(this.props.app.resolvers),
|
resolvers: objectToArray(this.props.app.resolvers),
|
||||||
connNode: null,
|
|
||||||
InputRaw: defaultSocketAddress
|
InputRaw: defaultSocketAddress
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,7 +25,16 @@ export default class SocketDebug extends React.Component{
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleDisconnectSocket(){
|
||||||
|
const socket = this.props.app.socket_conn
|
||||||
|
if (socket) {
|
||||||
|
console.log("closing")
|
||||||
|
socket.conn.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
render(){
|
render(){
|
||||||
|
const { socket_opt } = this.props.app
|
||||||
return(
|
return(
|
||||||
<div>
|
<div>
|
||||||
<antd.Card>
|
<antd.Card>
|
||||||
@ -42,12 +50,28 @@ export default class SocketDebug extends React.Component{
|
|||||||
/>
|
/>
|
||||||
</antd.Card>
|
</antd.Card>
|
||||||
|
|
||||||
connectedNode: {this.state.connNode}
|
{socket_opt?
|
||||||
|
<antd.List
|
||||||
|
grid={{
|
||||||
|
gutter: 5,
|
||||||
|
xxl: 3,
|
||||||
|
}}
|
||||||
|
dataSource={objectToArray(socket_opt)}
|
||||||
|
renderItem={(e) => {
|
||||||
|
return(
|
||||||
|
<div style={{ border: "0.1px rgb(217, 217, 217) solid" ,backgroundColor: "rgb(250, 250, 250)", borderRadius: "4px", width: "fit-content", paddingRight: "12px" }}>
|
||||||
|
<antd.Tag>{e.key}</antd.Tag>{JSON.stringify(e.value)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
: null}
|
||||||
|
|
||||||
<antd.Card>
|
<antd.Card>
|
||||||
<antd.Button onClick={() => this.setState({ InputRaw: defaultSocketAddress })} > Set default </antd.Button>
|
<antd.Button onClick={() => this.dispatchSocket(this.state.InputRaw)} > Connect </antd.Button>
|
||||||
<antd.Button onClick={() => this.dispatchSocket(this.state.InputRaw)} > connect </antd.Button>
|
<antd.Button onClick={() => this.handleDisconnectSocket()} > Disconnect </antd.Button>
|
||||||
<antd.Input onChange={(e) => this.setState({ InputRaw: e.target.value })} value={this.state.InputRaw} placeholder="ws:// http:// some.bruh:9090" />
|
<antd.Input onChange={(e) => this.setState({ InputRaw: e.target.value })} value={this.state.InputRaw} placeholder="ws:// http:// some.bruh:9090" />
|
||||||
|
<antd.Button onClick={() => this.setState({ InputRaw: defaultSocketAddress })} > Set default </antd.Button>
|
||||||
</antd.Card>
|
</antd.Card>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user