Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
RocketChat
Rocket.Chat.js.SDK
Commits
0253ff7a
Commit
0253ff7a
authored
Dec 03, 2021
by
Diego Mello
Browse files
Improve open and checkAndReopen
parent
3456c345
Changes
1
Hide whitespace changes
Inline
Side-by-side
lib/drivers/ddp.ts
View file @
0253ff7a
...
...
@@ -41,7 +41,6 @@ import { hostToWS } from '../util'
import
{
sha256
}
from
'
js-sha256
'
const
userDisconnectCloseCode
=
4000
;
const
reopenCloseCode
=
4001
;
/** Websocket handler class, manages connections and subscriptions by DDP */
export
class
Socket
extends
EventEmitter
{
...
...
@@ -52,7 +51,6 @@ export class Socket extends EventEmitter {
handlers
:
ISocketMessageHandler
[]
=
[]
config
:
ISocketOptions
|
any
openTimeout
?:
NodeJS
.
Timer
|
number
reopenInterval
?:
NodeJS
.
Timer
|
number
pingTimeout
?:
NodeJS
.
Timer
|
number
connection
?:
WebSocket
session
?:
string
...
...
@@ -92,15 +90,10 @@ export class Socket extends EventEmitter {
return
new
Promise
(
async
(
resolve
,
reject
)
=>
{
let
connection
:
WebSocket
if
(
this
.
connect
ion
)
{
this
.
connection
.
close
(
reopenCloseCode
)
if
(
this
.
connect
ed
)
{
return
resolve
(
)
}
this
.
reopenInterval
&&
clearInterval
(
this
.
reopenInterval
as
any
)
this
.
reopenInterval
=
setInterval
(()
=>
{
return
!
this
.
alive
()
&&
this
.
reopen
()
},
ms
)
try
{
connection
=
new
WebSocket
(
this
.
host
,
null
,
{
headers
:
settings
.
customHeaders
})
connection
.
onerror
=
reject
...
...
@@ -166,7 +159,6 @@ export class Socket extends EventEmitter {
close
=
async
()
=>
{
this
.
unsubscribeAll
().
catch
(
e
=>
this
.
logger
.
debug
(
e
))
this
.
reopenInterval
&&
clearInterval
(
this
.
reopenInterval
as
any
)
this
.
openTimeout
&&
clearTimeout
(
this
.
openTimeout
as
any
)
this
.
pingTimeout
&&
clearTimeout
(
this
.
pingTimeout
as
any
)
...
...
@@ -183,21 +175,25 @@ export class Socket extends EventEmitter {
return
Promise
.
resolve
()
}
// Call open directly, so it skips openTimeout
checkAndReopen
=
()
=>
{
if
(
!
this
.
connected
)
{
delete
this
.
openTimeout
this
.
reopen
()
if
(
this
.
openTimeout
)
{
clearTimeout
(
this
.
openTimeout
as
any
)
delete
this
.
openTimeout
}
this
.
open
()
}
}
/** Clear connection and try to connect again. */
reopen
=
async
()
=>
{
reopen
=
()
=>
{
if
(
this
.
openTimeout
)
return
this
.
openTimeout
=
setTimeout
(
async
()
=>
{
delete
this
.
openTimeout
try
{
await
this
.
open
()
await
this
.
open
()
}
catch
(
err
)
{
this
.
logger
.
error
(
`[ddp] Reopen error:
${
err
.
message
}
`
);
this
.
reopen
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment