|
| 1 | +//Package rpc provides abstract rpc server |
| 2 | +// |
| 3 | +//Copyright (C) 2022 Alexander Kiryukhin <[email protected]> |
| 4 | +// |
| 5 | +//This file is part of go.neonxp.dev/jsonrpc2 project. |
| 6 | +// |
| 7 | +//This program is free software: you can redistribute it and/or modify |
| 8 | +//it under the terms of the GNU General Public License as published by |
| 9 | +//the Free Software Foundation, either version 3 of the License, or |
| 10 | +//(at your option) any later version. |
| 11 | +// |
| 12 | +//This program is distributed in the hope that it will be useful, |
| 13 | +//but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +//GNU General Public License for more details. |
| 16 | +// |
| 17 | +//You should have received a copy of the GNU General Public License |
| 18 | +//along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +package transport |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "crypto/tls" |
| 25 | + "net" |
| 26 | + "net/http" |
| 27 | + "time" |
| 28 | + |
| 29 | + websocket "github.com/gobwas/ws" |
| 30 | + "github.com/gobwas/ws/wsutil" |
| 31 | +) |
| 32 | + |
| 33 | +type WebSocket struct { |
| 34 | + Bind string |
| 35 | + TLS *tls.Config |
| 36 | + CORSOrigin string |
| 37 | + Parallel bool |
| 38 | + ReadDeadline, WriteDeadline time.Duration //Set custom timeout for future read and write calls |
| 39 | +} |
| 40 | + |
| 41 | +func (ws *WebSocket) WithReadDealine() bool { return ws.ReadDeadline != 0 } |
| 42 | +func (ws *WebSocket) WithWriteDealine() bool { return ws.WriteDeadline != 0 } |
| 43 | + |
| 44 | +func (ws *WebSocket) Run(ctx context.Context, resolver Resolver) error { |
| 45 | + srv := http.Server{ |
| 46 | + Addr: ws.Bind, |
| 47 | + Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 48 | + wsconn, _, _, err := websocket.UpgradeHTTP(r, w) |
| 49 | + if err != nil { |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + defer func() { |
| 54 | + wsconn.Close() |
| 55 | + }() |
| 56 | + |
| 57 | + if ws.WithReadDealine() { |
| 58 | + wsconn.SetReadDeadline(time.Now().Add(ws.ReadDeadline * time.Second)) |
| 59 | + } |
| 60 | + |
| 61 | + if ws.WithWriteDealine() { |
| 62 | + wsconn.SetWriteDeadline(time.Now().Add(ws.WriteDeadline * time.Second)) |
| 63 | + } |
| 64 | + |
| 65 | + for { |
| 66 | + |
| 67 | + // read message from connection |
| 68 | + _, reader, err := wsutil.NextReader(wsconn, websocket.StateServerSide) |
| 69 | + if err != nil { |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + // create writer object that implements io.WriterCloser interface |
| 74 | + writer := wsutil.NewWriter(wsconn, websocket.StateServerSide, websocket.OpText) |
| 75 | + |
| 76 | + resolver.Resolve(ctx, reader, writer, ws.Parallel) |
| 77 | + |
| 78 | + if err := writer.Flush(); err != nil { |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + }), |
| 85 | + |
| 86 | + BaseContext: func(l net.Listener) context.Context { |
| 87 | + return ctx |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + go func() { |
| 92 | + <-ctx.Done() |
| 93 | + srv.Close() |
| 94 | + }() |
| 95 | + |
| 96 | + if err := srv.ListenAndServe(); err != http.ErrServerClosed { |
| 97 | + return err |
| 98 | + } |
| 99 | + return nil |
| 100 | +} |
0 commit comments