MCPcopy Create free account
hub / github.com/CPChain/chain / WebsocketHandler

Method WebsocketHandler

api/rpc/websocket.go:58–74  ·  view source on GitHub ↗

WebsocketHandler returns a handler that serves JSON-RPC to WebSocket connections. allowedOrigins should be a comma-separated list of allowed origin URLs. To allow connections with any origin, pass "*".

(allowedOrigins []string)

Source from the content-addressed store, hash-verified

56// allowedOrigins should be a comma-separated list of allowed origin URLs.
57// To allow connections with any origin, pass "*".
58func (srv *Server) WebsocketHandler(allowedOrigins []string) http.Handler {
59 return websocket.Server{
60 Handshake: wsHandshakeValidator(allowedOrigins),
61 Handler: func(conn *websocket.Conn) {
62 // Create a custom encode/decode pair to enforce payload size and number encoding
63 conn.MaxPayloadBytes = maxRequestContentLength
64
65 encoder := func(v interface{}) error {
66 return websocketJSONCodec.Send(conn, v)
67 }
68 decoder := func(v interface{}) error {
69 return websocketJSONCodec.Receive(conn, v)
70 }
71 srv.ServeCodec(NewCodec(conn, encoder, decoder), OptionMethodInvocation|OptionSubscriptions)
72 },
73 }
74}
75
76// NewWSServer creates a new websocket RPC server around an API provider.
77//

Callers 3

TestClientReconnectFunction · 0.80
httpTestClientFunction · 0.80
NewWSServerFunction · 0.80

Calls 3

ServeCodecMethod · 0.95
wsHandshakeValidatorFunction · 0.85
NewCodecFunction · 0.85

Tested by 2

TestClientReconnectFunction · 0.64
httpTestClientFunction · 0.64