good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
J
jsonpatch
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
jsonpatch
Commits
04e236d2
Commit
04e236d2
authored
Aug 10, 2022
by
a
Browse files
Options
Downloads
Patches
Plain Diff
updates mayve:
parent
1be5f024
Branches
Branches containing commit
Tags
v0.0.7
Tags containing commit
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
stream/stream.go
+4
-0
4 additions, 0 deletions
stream/stream.go
stream/websocket_client.go
+8
-3
8 additions, 3 deletions
stream/websocket_client.go
stream/websocket_server.go
+21
-15
21 additions, 15 deletions
stream/websocket_server.go
with
33 additions
and
18 deletions
stream/stream.go
+
4
−
0
View file @
04e236d2
...
@@ -74,6 +74,10 @@ func (s *Stream) Sub(sb Subscriber) string {
...
@@ -74,6 +74,10 @@ func (s *Stream) Sub(sb Subscriber) string {
return
o
.
id
return
o
.
id
}
}
func
(
s
*
Stream
)
Unsubscribe
(
id
string
)
{
s
.
subs
.
Delete
(
id
)
}
func
(
s
*
Stream
)
run
()
{
func
(
s
*
Stream
)
run
()
{
go
s
.
run_update
()
go
s
.
run_update
()
go
s
.
run_send
()
go
s
.
run_send
()
...
...
This diff is collapsed.
Click to expand it.
stream/websocket_client.go
+
8
−
3
View file @
04e236d2
...
@@ -54,7 +54,7 @@ type cmd struct {
...
@@ -54,7 +54,7 @@ type cmd struct {
Topic
string
`json:"topic"`
Topic
string
`json:"topic"`
Patch
gjson
.
RawMessage
`json:"patch"`
Patch
gjson
.
RawMessage
`json:"patch"`
Seq
int
`json:"seq"`
Seq
int
`json:"seq"`
}
}
`json:"params"`
}
}
func
(
ws
*
WebsocketClient
)
connect
()
(
err
error
)
{
func
(
ws
*
WebsocketClient
)
connect
()
(
err
error
)
{
...
@@ -134,8 +134,13 @@ func (w *Receiver) Seq() int {
...
@@ -134,8 +134,13 @@ func (w *Receiver) Seq() int {
return
w
.
seq
return
w
.
seq
}
}
type
sub_cmd
struct
{
Method
string
`json:"method"`
Params
[]
string
`json:"params"`
}
func
(
w
*
Receiver
)
subscribe
()
error
{
func
(
w
*
Receiver
)
subscribe
()
error
{
return
w
.
c
.
c
.
WriteJSON
(
map
[
string
]
any
{
"m
ethod
"
:
"topic_subscribe"
,
"p
arams
"
:
[]
string
{
w
.
topic
}})
return
w
.
c
.
c
.
WriteJSON
(
sub_cmd
{
M
ethod
:
"topic_subscribe"
,
P
arams
:
[]
string
{
w
.
topic
}})
}
}
func
(
w
*
Receiver
)
OnPatch
(
seq
int
,
diffs
[]
byte
)
error
{
func
(
w
*
Receiver
)
OnPatch
(
seq
int
,
diffs
[]
byte
)
error
{
...
@@ -159,7 +164,7 @@ func (w *Receiver) OnPatchRaw(seq int, diffs gjson.RawMessage) error {
...
@@ -159,7 +164,7 @@ func (w *Receiver) OnPatchRaw(seq int, diffs gjson.RawMessage) error {
}
}
func
(
w
*
Receiver
)
Close
()
{
func
(
w
*
Receiver
)
Close
()
{
w
.
c
.
c
.
WriteJSON
(
map
[
string
]
any
{
"topic"
:
"topic_unsubscribe"
,
"p
arams
"
:
[]
string
{
w
.
topic
}})
w
.
c
.
c
.
WriteJSON
(
sub_cmd
{
Method
:
"topic_unsubscribe"
,
P
arams
:
[]
string
{
w
.
topic
}})
w
.
c
.
Lock
()
w
.
c
.
Lock
()
defer
w
.
c
.
Unlock
()
defer
w
.
c
.
Unlock
()
delete
(
w
.
c
.
Streams
,
w
.
topic
)
delete
(
w
.
c
.
Streams
,
w
.
topic
)
...
...
This diff is collapsed.
Click to expand it.
stream/websocket.go
→
stream/websocket
_server
.go
+
21
−
15
View file @
04e236d2
...
@@ -4,8 +4,8 @@ import (
...
@@ -4,8 +4,8 @@ import (
"context"
"context"
"log"
"log"
"net/http"
"net/http"
"sync"
"gfx.cafe/open/goutil/generic"
"gfx.cafe/open/jsonpatch"
"gfx.cafe/open/jsonpatch"
"gfx.cafe/open/jsonrpc2"
"gfx.cafe/open/jsonrpc2"
"gfx.cafe/open/jsonrpc2/external/websocket"
"gfx.cafe/open/jsonrpc2/external/websocket"
...
@@ -14,10 +14,8 @@ import (
...
@@ -14,10 +14,8 @@ import (
var
DefaultUpgrader
=
websocket
.
Upgrader
{}
// use default options
var
DefaultUpgrader
=
websocket
.
Upgrader
{}
// use default options
type
WebsocketServer
struct
{
type
WebsocketServer
struct
{
Streams
map
[
string
]
*
Stream
Streams
generic
.
MapOf
[
string
,
*
Stream
]
u
websocket
.
Upgrader
u
websocket
.
Upgrader
mu
sync
.
RWMutex
}
}
func
NewWssServer
(
upgrader
...
websocket
.
Upgrader
)
*
WebsocketServer
{
func
NewWssServer
(
upgrader
...
websocket
.
Upgrader
)
*
WebsocketServer
{
...
@@ -26,7 +24,6 @@ func NewWssServer(upgrader ...websocket.Upgrader) *WebsocketServer {
...
@@ -26,7 +24,6 @@ func NewWssServer(upgrader ...websocket.Upgrader) *WebsocketServer {
u
=
upgrader
[
0
]
u
=
upgrader
[
0
]
}
}
return
&
WebsocketServer
{
return
&
WebsocketServer
{
Streams
:
make
(
map
[
string
]
*
Stream
),
u
:
u
,
u
:
u
,
}
}
}
}
...
@@ -43,6 +40,11 @@ func (ws *WebsocketServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
...
@@ -43,6 +40,11 @@ func (ws *WebsocketServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
return
}
}
defer
c
.
Close
()
defer
c
.
Close
()
ws
.
handleClient
(
c
,
w
,
r
)
}
func
(
ws
*
WebsocketServer
)
handleClient
(
c
*
websocket
.
Conn
,
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
routes
:=
map
[
string
]
string
{}
for
{
for
{
_
,
message
,
err
:=
c
.
ReadMessage
()
_
,
message
,
err
:=
c
.
ReadMessage
()
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -62,27 +64,31 @@ func (ws *WebsocketServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
...
@@ -62,27 +64,31 @@ func (ws *WebsocketServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
continue
continue
}
}
switch
cmd
.
Method
{
switch
cmd
.
Method
{
case
"close"
:
break
case
"topic_subscribe"
:
case
"topic_subscribe"
:
for
_
,
v
:=
range
cmd
.
Params
{
for
_
,
v
:=
range
cmd
.
Params
{
ws
.
mu
.
RLock
()
if
st
,
ok
:=
ws
.
Streams
.
Load
(
v
);
ok
{
if
st
,
ok
:=
ws
.
Streams
[
v
];
ok
{
ws
.
mu
.
RUnlock
()
sub
:=
NewWssSubscriber
(
r
.
Context
(),
v
,
c
)
sub
:=
NewWssSubscriber
(
r
.
Context
(),
v
,
c
)
st
.
Sub
(
sub
)
xid_str
:=
st
.
Sub
(
sub
)
}
else
{
routes
[
v
]
=
xid_str
ws
.
mu
.
RUnlock
()
}
}
}
}
case
"topic_unsubscribe"
:
case
"topic_unsubscribe"
:
for
_
,
v
:=
range
cmd
.
Params
{
if
id
,
ok
:=
routes
[
v
];
ok
{
if
st
,
ok
:=
ws
.
Streams
.
Load
(
v
);
ok
{
st
.
Unsubscribe
(
id
)
}
}
}
}
}
}
}
}
}
func
ServerRegister
[
T
any
](
w
*
WebsocketServer
,
route
string
,
v
T
)
*
TStream
[
T
]
{
func
ServerRegister
[
T
any
](
w
*
WebsocketServer
,
route
string
,
v
T
)
*
TStream
[
T
]
{
o
:=
New
()
o
:=
New
()
w
.
mu
.
Lock
()
w
.
Streams
.
Store
(
route
,
o
)
defer
w
.
mu
.
Unlock
()
w
.
Streams
[
route
]
=
o
return
&
TStream
[
T
]{
return
&
TStream
[
T
]{
o
,
o
,
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment