Cosmos Nicolaou | 3c50ac4 | 2014-12-23 07:40:19 -0800 | [diff] [blame] | 1 | package websocket |
| 2 | |
| 3 | import ( |
| 4 | "net" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | var mapWebSocketToTCP = map[string]string{"ws": "tcp", "ws4": "tcp4", "ws6": "tcp6", "wsh": "tcp", "wsh4": "tcp4", "wsh6": "tcp6", "tcp": "tcp", "tcp4": "tcp4", "tcp6": "tcp6"} |
| 9 | |
| 10 | // HybridDial returns net.Conn that can be used with a HybridListener but |
| 11 | // always uses tcp. A client must specifically elect to use websockets by |
| 12 | // calling websocket.Dialer. The returned net.Conn will report 'tcp' as its |
| 13 | // Network. |
| 14 | func HybridDial(network, address string, timeout time.Duration) (net.Conn, error) { |
| 15 | tcp := mapWebSocketToTCP[network] |
| 16 | return net.DialTimeout(tcp, address, timeout) |
| 17 | } |
| 18 | |
| 19 | // HybridListener returns a net.Listener that supports both tcp and |
| 20 | // websockets over the same, single, port. A listen address of |
| 21 | // --veyron.tcp.protocol=wsh --veyron.tcp.address=127.0.0.1:8101 means |
| 22 | // that port 8101 can accept connections that use either tcp or websocket. |
| 23 | // The listener looks at the first 4 bytes of the incoming data stream |
| 24 | // to decide if it's a websocket protocol or not. These must be 'GET ' for |
| 25 | // websockets, all other protocols must guarantee to not send 'GET ' as the |
| 26 | // first four bytes of the payload. |
| 27 | func HybridListener(protocol, address string) (net.Listener, error) { |
| 28 | return listener(protocol, address, true) |
| 29 | } |