This repository has been archived by the owner on Aug 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpws.go
69 lines (55 loc) · 1.69 KB
/
httpws.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package net
import (
"github.com/vocdoni/mtrouter/types"
)
// HttpWsHandler is a Mixed handler websockets/http
type HttpWsHandler struct {
Proxy *Proxy // proxy where the ws will be associated
Connection *types.Connection // the ws connection
internalReceiver chan types.Message
}
func (h *HttpWsHandler) Init(c *types.Connection) error {
h.internalReceiver = make(chan types.Message, 1)
return nil
}
func (h *HttpWsHandler) SetProxy(p *Proxy) {
h.Proxy = p
}
// AddProxyHandler adds the current websocket handler into the Proxy
func (h *HttpWsHandler) AddProxyHandler(path string) {
h.Proxy.AddMixedHandler(path, getHTTPhandler(path, h.internalReceiver), getWsHandler(path, h.internalReceiver))
}
func (h *HttpWsHandler) ConnectionType() string {
return "HTTPWS"
}
func (h *HttpWsHandler) Listen(receiver chan<- types.Message) {
for {
msg := <-h.internalReceiver
receiver <- msg
}
}
func (h *HttpWsHandler) SendUnicast(address string, msg types.Message) {
// WebSocket is not p2p so sendUnicast makes the same of Send()
h.Send(msg)
}
func (h *HttpWsHandler) Send(msg types.Message) {
// TODO(mvdan): this extra abstraction layer is probably useless
msg.Context.(*HttpContext).Send(msg)
}
func (h *HttpWsHandler) SetBootnodes(bootnodes []string) {
// No bootnodes on websockets handler
}
func (h *HttpWsHandler) AddPeer(peer string) error {
// No peers on websockets handler
return nil
}
// AddNamespace adds a new namespace to the transport
func (h *HttpWsHandler) AddNamespace(namespace string) {
h.AddProxyHandler(namespace)
}
func (h *HttpWsHandler) Address() string {
return h.String()
}
func (h *HttpWsHandler) String() string {
return h.Proxy.Addr.String()
}