Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
frontend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
frontend
Commits
dc8dce68
Commit
dc8dce68
authored
Nov 02, 2022
by
tom
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
basic socket implementation
parent
b13780fe
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
120 additions
and
1 deletion
+120
-1
getCspPolicy.ts
lib/csp/getCspPolicy.ts
+3
-0
useApiSocket.tsx
lib/hooks/useApiSocket.tsx
+103
-0
Home.tsx
ui/pages/Home.tsx
+14
-1
No files found.
lib/csp/getCspPolicy.ts
View file @
dc8dce68
...
...
@@ -61,6 +61,9 @@ function makePolicyMap() {
// client error monitoring
'
sentry.io
'
,
'
*.sentry.io
'
,
// todo_tom pass host from config
'
wss://blockscout.com
'
,
],
'
script-src
'
:
[
...
...
lib/hooks/useApiSocket.tsx
0 → 100644
View file @
dc8dce68
import
React
from
'
react
'
;
import
{
SECOND
}
from
'
lib/consts
'
;
const
OPEN_STATE
=
1
;
interface
Params
{
onOpen
?:
(
event
:
Event
)
=>
void
;
onError
?:
(
event
:
Event
)
=>
void
;
onClose
?:
(
event
:
Event
)
=>
void
;
}
type
SocketData
=
[
null
,
null
,
string
,
string
,
Record
<
string
,
unknown
>
];
interface
SocketChannelSubscriber
{
filters
?:
Array
<
string
>
;
callback
:
(
payload
:
unknown
)
=>
void
;
}
export
default
function
useApiSocket
({
onOpen
,
onError
,
onClose
}:
Params
)
{
const
socket
=
React
.
useRef
<
WebSocket
>
();
const
onReadyEvents
=
React
.
useRef
<
Array
<
SocketData
>>
([]);
const
channels
=
React
.
useRef
<
Record
<
string
,
Array
<
SocketChannelSubscriber
>>>
({});
function
startHeartBeat
()
{
return
window
.
setInterval
(()
=>
{
const
data
:
SocketData
=
[
null
,
null
,
'
phoenix
'
,
'
heartbeat
'
,
{}
];
socket
.
current
?.
send
(
JSON
.
stringify
(
data
));
},
30
*
SECOND
);
}
const
joinRoom
=
React
.
useCallback
((
id
:
string
,
subscriber
:
SocketChannelSubscriber
)
=>
{
const
data
:
SocketData
=
[
null
,
null
,
id
,
'
phx_join
'
,
{}
];
if
(
socket
.
current
?.
readyState
===
OPEN_STATE
)
{
socket
.
current
?.
send
(
JSON
.
stringify
(
data
));
}
else
{
onReadyEvents
.
current
.
push
(
data
);
}
if
(
channels
.
current
[
id
])
{
channels
.
current
[
id
].
push
(
subscriber
);
}
else
{
channels
.
current
[
id
]
=
[
subscriber
];
}
},
[]);
const
leaveRoom
=
React
.
useCallback
((
id
:
string
)
=>
{
const
data
:
SocketData
=
[
null
,
null
,
id
,
'
phx_leave
'
,
{}
];
if
(
socket
.
current
?.
readyState
===
OPEN_STATE
)
{
socket
.
current
?.
send
(
JSON
.
stringify
(
data
));
}
else
{
onReadyEvents
.
current
.
push
(
data
);
}
channels
.
current
[
id
]
=
[];
},
[]);
React
.
useEffect
(()
=>
{
if
(
socket
.
current
)
{
socket
.
current
.
close
();
}
// todo_tom pass host and base path from config
socket
.
current
=
new
WebSocket
(
'
wss://blockscout.com/poa/core/socket/v2/websocket?vsn=2.0.0
'
);
let
heartBeatTimeoutId
:
number
|
undefined
;
socket
.
current
.
addEventListener
(
'
open
'
,
(
event
:
Event
)
=>
{
onOpen
?.(
event
);
heartBeatTimeoutId
=
startHeartBeat
();
onReadyEvents
.
current
.
forEach
((
data
)
=>
socket
.
current
?.
send
(
JSON
.
stringify
(
data
)));
onReadyEvents
.
current
=
[];
});
socket
.
current
.
addEventListener
(
'
message
'
,
(
event
)
=>
{
const
data
:
SocketData
=
JSON
.
parse
(
event
.
data
);
const
channelId
=
data
[
2
];
const
filterId
=
data
[
3
];
const
payload
=
data
[
4
];
const
subscribers
=
channels
.
current
[
channelId
];
subscribers
?.
filter
((
subscriber
)
=>
subscriber
.
filters
?
subscriber
.
filters
.
includes
(
filterId
)
:
true
)
?.
forEach
((
subscriber
)
=>
subscriber
.
callback
(
payload
));
});
socket
.
current
.
addEventListener
(
'
error
'
,
(
event
)
=>
{
onError
?.(
event
);
});
socket
.
current
.
addEventListener
(
'
close
'
,
(
event
)
=>
{
onClose
?.(
event
);
});
return
()
=>
{
window
.
clearInterval
(
heartBeatTimeoutId
);
socket
.
current
?.
close
();
};
},
[
onClose
,
onError
,
onOpen
]);
return
{
joinRoom
,
leaveRoom
};
}
ui/pages/Home.tsx
View file @
dc8dce68
...
...
@@ -5,6 +5,7 @@ import React from 'react';
import
appConfig
from
'
configs/app/config
'
;
import
*
as
cookies
from
'
lib/cookies
'
;
import
useApiSocket
from
'
lib/hooks/useApiSocket
'
;
import
useToast
from
'
lib/hooks/useToast
'
;
import
Page
from
'
ui/shared/Page/Page
'
;
import
PageTitle
from
'
ui/shared/Page/PageTitle
'
;
...
...
@@ -14,11 +15,23 @@ const Home = () => {
const
[
isFormVisible
,
setFormVisibility
]
=
React
.
useState
(
false
);
const
[
token
,
setToken
]
=
React
.
useState
(
''
);
const
{
joinRoom
,
leaveRoom
}
=
useApiSocket
({});
React
.
useEffect
(()
=>
{
joinRoom
(
'
blocks:0xdc4765d9dabf6c6c4908fe97e649ef1f05cb6252
'
,
{
filters
:
[
'
new_block
'
],
callback
:
()
=>
{},
});
return
()
=>
{
leaveRoom
(
'
blocks:0xdc4765d9dabf6c6c4908fe97e649ef1f05cb6252
'
);
};
},
[
joinRoom
,
leaveRoom
]);
React
.
useEffect
(()
=>
{
const
token
=
cookies
.
get
(
cookies
.
NAMES
.
API_TOKEN
);
setFormVisibility
(
Boolean
(
!
token
&&
appConfig
.
isAccountSupported
));
},
[]);
},
[
joinRoom
]);
const
checkSentry
=
React
.
useCallback
(()
=>
{
Sentry
.
captureException
(
new
Error
(
'
Test error
'
),
{
extra
:
{
foo
:
'
bar
'
},
tags
:
{
source
:
'
test
'
}
});
...
...
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