socket init on query model

This commit is contained in:
srgooglo 2020-10-20 21:49:25 +02:00
parent 052d3b58c7
commit 674d270fa6
3 changed files with 67 additions and 48 deletions

View File

@ -1,14 +1,11 @@
import io from 'socket.io-client'
import verbosity from 'core/libs/verbosity'
import { connect } from 'umi'
import settings from 'core/libs/settings'
import { notify } from 'core/libs/appInterface'
const maxDeep_Attemp = Number(2)
export class SocketConnection{
export default class SocketConnection{
ioConn: any
state: { address: any; connAttemps: number; registeredNamespaces: any; }
state: { connAttemps: number; registeredNamespaces: any; }
props: any
opts: any
@ -16,22 +13,23 @@ export class SocketConnection{
if (!props) {
throw new Error("Mmm some props are not defined")
}
this.props = props.payload
this.dispatcher = props.connector
this.state = {
address: props.payload.address,
connAttemps: Number(0),
registeredNamespaces: []
}
this.props = props
this.opts = {
hostname: "localhost:5000",
reconnection: true,
reconnectionAttempts: maxDeep_Attemp,
reconnectionAttempts: Number(2),
reconnectionDelay: 1000,
reconnectionDelayMax: 5000,
randomizationFactor: 0.5,
timeout: 20000,
autoConnect: false,
autoConnect: true,
query: {},
// options of the Engine.IO client
upgrade: true,
@ -64,21 +62,27 @@ export class SocketConnection{
extraHeaders: {},
}
this.ioConn = io(this.state.address, this.opts)
this.conn.open()
if (typeof(this.props) !== "undefined") {
this.opts = { ...this.opts, ...this.props}
}
verbosity([`New socket connection, with parameters =>`, this.opts], { color: "blue" })
this.ioConn = io(this.opts.hostname, this.opts)
this.ioConn.on('connect', (event:any) => {
notify.success("You are now online")
verbosity("Successfully connect")
props.then(true) // this send an signal when the socket its successfully connected
})
this.ioConn.on("connect_error", () => {
if (this.state.connAttemps >= maxDeep_Attemp) {
verbosity(['Maximun nº of attemps reached => max', maxDeep_Attemp + 1])
this.conn.close()
this.ioConn.on("connect_error", (event:any) => {
if (this.state.connAttemps >= this.opts.reconnectionAttempts) {
verbosity(['Maximun nº of attemps reached => max', this.opts.reconnectionAttempts + 1])
this.ioConn.close()
return false
}
verbosity([`Strike [${this.state.connAttemps + 1}] / ${maxDeep_Attemp + 1} !`])
verbosity([`Strike [${this.state.connAttemps + 1}] / ${this.opts.reconnectionAttempts + 1} !`, event])
this.state.connAttemps = this.state.connAttemps + 1
})
@ -101,7 +105,7 @@ export class SocketConnection{
this.ioConn.on('updateState', (event:any) => {
verbosity(["updating state > ", event])
this.props.connector.dispatcher({ type: "updateState", payload: event })
this.dispatcher({ type: "socket/updateState", payload: event })
})
this.ioConn.on('pingPong', (e:any) => {
@ -110,23 +114,7 @@ export class SocketConnection{
const fart = new Audio("https://dl.ragestudio.net/pedo_cum.mp3")
fart.play()
setTimeout(() => { this.ioConn.emit("pingPong", n) }, n)
})
}
conn = {
open: () => {
this.ioConn.open()
},
disconnect: () => {
this.ioConn.disconnect()
},
close: () => {
this.ioConn.close()
},
destroy: () => {
}
})
}
}

View File

@ -16,8 +16,7 @@ export default {
namespace: 'app',
state: {
env_proccess: process.env,
socket_conn: null,
socket_opt: null,
socket_address: "localhost:7000", //set by default
server_key: keys.server_key,
resolvers: null,
@ -34,6 +33,7 @@ export default {
overlayActive: false,
overlayElement: null,
embedded: false,
dispatcher: null,
controlActive: false,
feedOutdated: false,
@ -45,6 +45,7 @@ export default {
},
subscriptions: {
setup({ dispatch }) {
dispatch({ type: 'updateState', payload: { dispatcher: dispatch } })
try {
const electron = window.require("electron")
dispatch({ type: 'updateState', payload: { electron, embedded: true } })
@ -56,7 +57,8 @@ export default {
})
dispatch({ type: 'updateFrames' })
dispatch({ type: 'handleValidate' })
dispatch({ type: 'query', payload: { dispatcher: dispatch } });
dispatch({ type: 'queryAuth' })
dispatch({ type: 'query', payload: { dispatcher: dispatch } })
},
setupHistory({ dispatch, history }) {
history.listen(location => {
@ -115,10 +117,27 @@ export default {
}
if (!sessionDataframe && session ) {
yield put({ type: 'handleGetUserData' })
}
},
*queryAuth({ payload }, { call, put, select }) {
const socket = yield select(state => state.socket)
const state = yield select(state => state)
verbosity([`Starting Auth process`])
yield put({ type: 'socket/initializeSocket', payload: {
hostname: state.app.socket_address,
reconnectionAttempts: 10
}, then: () => {
//socket.socket_conn.emit("pingPong")
}})
},
*logout({ payload }, { call, put, select }) {
const uuid = yield select(state => state.app.session_uuid)

View File

@ -6,7 +6,7 @@ import { router, verbosity, appInterface } from 'core/libs'
import settings from 'core/libs/settings'
import { __legacy__objectToArray } from 'core'
import { SocketConnection, SocketModel } from 'core/libs/socket/index.ts'
import SocketConnection from 'core/libs/socket/index.ts'
import jwt from 'jsonwebtoken'
import cookie from 'cookie_js'
@ -16,28 +16,34 @@ const defaultSocketAddress = "localhost:7000"
export default {
namespace: 'socket',
state: {
dispatcher: null,
resolvers: null,
socket_conn: null
ioConn: null
},
subscriptions: {
setup({ dispatch }) {
dispatch({ type: 'updateState', payload: { dispatcher: dispatch } })
dispatch({ type: 'query' })
},
},
effects: {
*query({ payload }, { call, put, select }) {
const stateConnector = yield select(state => state)
const state = yield select(state => state)
yield put({ type: "updateState", payload: { resolvers: stateConnector.app.resolvers } })
yield put({ type: "updateState", payload: { resolvers: state.app.resolvers } })
},
*initializeSocket({payload}, {select, put}){
*initializeSocket({payload, then}, {select, put}){
if(!payload) return false
const stateConnector = yield select(state => state)
const state = yield select(state => state)
const handleThen = () => {
if (typeof(then) !== "undefined") {
then(true)
}
}
yield put({ type: "handleSocket", payload: new SocketConnection({payload, connector: stateConnector.socket}) })
yield put({
type: "handleSocket",
payload: new SocketConnection({payload, connector: state.app.dispatcher, then: handleThen })
})
},
},
reducers: {
@ -48,8 +54,14 @@ export default {
};
},
handleSocket(state, { payload }) {
state.socket_conn = payload
state.socket_opt = payload.opts
console.log(payload.ioConn)
state.ioConn = payload.ioConn
state.ioConn.json = null // avoiding circular...
state.ioConn.nsps = null
state.ioConn.io.nsps = null
state.ioConn.io.connecting = null
//state.ioConn.io.opts.engine.transport = null
},
},
};