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)
| 56 | // allowedOrigins should be a comma-separated list of allowed origin URLs. |
| 57 | // To allow connections with any origin, pass "*". |
| 58 | func (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 | // |