more stable p2p v2 connection to server
parent
d4eb4bafdd
commit
68225a63b1
|
@ -45,19 +45,31 @@ function startConnection(p2pServerAddress,subscriptionId){
|
||||||
stayDisconnected = false
|
stayDisconnected = false
|
||||||
const allMessageHandlers = []
|
const allMessageHandlers = []
|
||||||
async function startWebsocketConnection(key,callback){
|
async function startWebsocketConnection(key,callback){
|
||||||
|
s.debugLog(`startWebsocketConnection EXECUTE`,new Error())
|
||||||
function createWebsocketConnection(){
|
function createWebsocketConnection(){
|
||||||
return new Promise((resolve,reject) => {
|
return new Promise((resolve,reject) => {
|
||||||
const newTunnel = new WebSocket(p2pServerAddress || 'ws://172.16.101.218:81');
|
try{
|
||||||
newTunnel.on('open', function(){
|
stayDisconnected = true
|
||||||
resolve(newTunnel)
|
if(tunnelToShinobi)tunnelToShinobi.close()
|
||||||
|
}catch(err){
|
||||||
|
console.log(err)
|
||||||
|
}
|
||||||
|
tunnelToShinobi = new WebSocket(p2pServerAddress || 'ws://172.16.101.218:81');
|
||||||
|
stayDisconnected = false;
|
||||||
|
tunnelToShinobi.on('open', function(){
|
||||||
|
resolve(tunnelToShinobi)
|
||||||
})
|
})
|
||||||
newTunnel.on('error', (err) => {
|
tunnelToShinobi.on('error', (err) => {
|
||||||
console.log(`P2P newTunnel Error : `,err)
|
console.log(`P2P tunnelToShinobi Error : `,err)
|
||||||
console.log(`P2P Restarting...`)
|
console.log(`P2P Restarting...`)
|
||||||
disconnectedConnection()
|
disconnectedConnection()
|
||||||
})
|
})
|
||||||
newTunnel.on('close', disconnectedConnection);
|
tunnelToShinobi.on('close', () => {
|
||||||
newTunnel.onmessage = function(event){
|
setTimeout(() => {
|
||||||
|
if(tunnelToShinobi.readyState !== 1)disconnectedConnection();
|
||||||
|
},5000)
|
||||||
|
});
|
||||||
|
tunnelToShinobi.onmessage = function(event){
|
||||||
const data = bson.deserialize(Buffer.from(event.data))
|
const data = bson.deserialize(Buffer.from(event.data))
|
||||||
allMessageHandlers.forEach((handler) => {
|
allMessageHandlers.forEach((handler) => {
|
||||||
if(data.f === handler.key){
|
if(data.f === handler.key){
|
||||||
|
@ -68,8 +80,8 @@ function startConnection(p2pServerAddress,subscriptionId){
|
||||||
|
|
||||||
clearInterval(socketCheckTimer)
|
clearInterval(socketCheckTimer)
|
||||||
socketCheckTimer = setInterval(() => {
|
socketCheckTimer = setInterval(() => {
|
||||||
s.debugLog('Tunnel Ready State :',newTunnel.readyState)
|
s.debugLog('Tunnel Ready State :',tunnelToShinobi.readyState)
|
||||||
if(newTunnel.readyState !== 1){
|
if(tunnelToShinobi.readyState !== 1){
|
||||||
s.debugLog('Tunnel NOT Ready! Reconnecting...')
|
s.debugLog('Tunnel NOT Ready! Reconnecting...')
|
||||||
disconnectedConnection()
|
disconnectedConnection()
|
||||||
}
|
}
|
||||||
|
@ -80,17 +92,12 @@ function startConnection(p2pServerAddress,subscriptionId){
|
||||||
s.debugLog('stayDisconnected',stayDisconnected)
|
s.debugLog('stayDisconnected',stayDisconnected)
|
||||||
if(stayDisconnected)return;
|
if(stayDisconnected)return;
|
||||||
s.debugLog('DISCONNECTED! RESTARTING!')
|
s.debugLog('DISCONNECTED! RESTARTING!')
|
||||||
setTimeout(() => {
|
// setTimeout(() => {
|
||||||
startWebsocketConnection()
|
startWebsocketConnection()
|
||||||
},2000)
|
// },2000)
|
||||||
}
|
|
||||||
try{
|
|
||||||
if(tunnelToShinobi)tunnelToShinobi.close()
|
|
||||||
}catch(err){
|
|
||||||
console.log(err)
|
|
||||||
}
|
}
|
||||||
s.debugLog(p2pServerAddress)
|
s.debugLog(p2pServerAddress)
|
||||||
tunnelToShinobi = await createWebsocketConnection(p2pServerAddress,allMessageHandlers)
|
await createWebsocketConnection(p2pServerAddress,allMessageHandlers)
|
||||||
console.log('P2P : Connected! Authenticating...')
|
console.log('P2P : Connected! Authenticating...')
|
||||||
sendDataToTunnel({
|
sendDataToTunnel({
|
||||||
subscriptionId: subscriptionId
|
subscriptionId: subscriptionId
|
||||||
|
|
Loading…
Reference in New Issue