1
- /*
2
- * Copyright 2019-2023 SURF.
3
- * Licensed under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License.
5
- * You may obtain a copy of the License at
6
- * http://www.apache.org/licenses/LICENSE-2.0
7
- *
8
- * Unless required by applicable law or agreed to in writing, software
9
- * distributed under the License is distributed on an "AS IS" BASIS,
10
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
- * See the License for the specific language governing permissions and
12
- * limitations under the License.
13
- *
14
- */
15
-
16
1
import { useAuth } from "oidc-react" ;
17
2
import { useEffect , useState } from "react" ;
18
3
import useWebSocket , { Options , ReadyState } from "react-use-websocket" ;
19
4
20
5
import { ENV } from "../env" ;
21
6
22
7
const isSecure = ! ! ENV . BACKEND_URL . includes ( "https" ) ;
23
-
24
8
const wsProtocol = isSecure ? "wss" : "ws" ;
25
9
const websocketUrl = `${ wsProtocol } ${ ENV . BACKEND_URL . replace ( / ( ^ \w + | ^ ) / , "" ) } ` ;
26
10
@@ -30,6 +14,9 @@ export interface SurfWebSocket<T> {
30
14
useFallback : boolean ;
31
15
}
32
16
17
+ const pingMessage = "__ping__" ;
18
+ const pingintervalInMilliseconds = ENV . WS_PING_INTERVAL_IN_SECONDS * 1000 ;
19
+
33
20
const websocketSettings : Options = {
34
21
retryOnError : true ,
35
22
reconnectAttempts : 10 ,
@@ -46,7 +33,7 @@ const useWebsocketServiceWithAuth = <T extends object>(endpoint: string): SurfWe
46
33
const baseUrl = `${ websocketUrl } /${ endpoint } ?token=` ;
47
34
const [ url , setUrl ] = useState < string > ( "" ) ;
48
35
49
- const { lastJsonMessage, readyState } = useWebSocket ( url , websocketSettings , ! ! url ) ;
36
+ const { lastJsonMessage, readyState, sendMessage , lastMessage } = useWebSocket ( url , websocketSettings , ! ! url ) ;
50
37
const [ useFallback , setUsefallback ] = useState < boolean > ( false ) ;
51
38
52
39
useEffect ( ( ) => {
@@ -57,23 +44,44 @@ const useWebsocketServiceWithAuth = <T extends object>(endpoint: string): SurfWe
57
44
setUrl ( `${ baseUrl } ${ auth . userData ?. access_token } ` ) ;
58
45
} , [ baseUrl , auth . userData ?. access_token ] ) ;
59
46
47
+ useEffect ( ( ) => {
48
+ const pingInterval = setInterval ( ( ) => sendMessage ( pingMessage ) , pingintervalInMilliseconds ) ;
49
+ return ( ) => clearInterval ( pingInterval ) ;
50
+ } , [ sendMessage ] ) ;
51
+
52
+ useEffect ( ( ) => {
53
+ if ( lastMessage && lastMessage . data === "__pong__" ) {
54
+ // handle pong...
55
+ }
56
+ } , [ lastMessage ] ) ;
57
+
60
58
return { lastMessage : lastJsonMessage , readyState, useFallback } ;
61
59
} ;
62
60
63
61
const useWebsocketServiceWithoutAuth = < T extends object > ( endpoint : string ) : SurfWebSocket < T > => {
64
62
const baseUrl = `${ websocketUrl } /${ endpoint } ?token=` ;
65
- const { lastJsonMessage, readyState } = useWebSocket ( baseUrl , websocketSettings ) ;
63
+ const { lastJsonMessage, readyState, sendMessage , lastMessage } = useWebSocket ( baseUrl , websocketSettings ) ;
66
64
const [ useFallback , setUsefallback ] = useState < boolean > ( false ) ;
67
65
68
66
useEffect ( ( ) => {
69
- console . log ( readyState ) ;
70
67
if ( readyState === ReadyState . CLOSED ) {
71
68
setUsefallback ( true ) ;
72
69
} else {
73
70
setUsefallback ( false ) ;
74
71
}
75
72
} , [ readyState ] ) ;
76
73
74
+ useEffect ( ( ) => {
75
+ const pingInterval = setInterval ( ( ) => sendMessage ( pingMessage ) , pingintervalInMilliseconds ) ;
76
+ return ( ) => clearInterval ( pingInterval ) ;
77
+ } , [ sendMessage ] ) ;
78
+
79
+ useEffect ( ( ) => {
80
+ if ( lastMessage && lastMessage . data === "__pong__" ) {
81
+ // handle pong...
82
+ }
83
+ } , [ lastMessage ] ) ;
84
+
77
85
return { lastMessage : lastJsonMessage , readyState, useFallback } ;
78
86
} ;
79
87
0 commit comments