Changeset 323 in code for trunk/conn.go


Ignore:
Timestamp:
Jun 7, 2020, 12:13:46 PM (5 years ago)
Author:
contact
Message:

Add support for WebSocket connections

WebSocket connections allow web-based clients to connect to IRC. This
commit implements the WebSocket sub-protocol as specified by the pending
IRCv3 proposal [1].

WebSocket listeners can now be set up via a "wss" protocol in the
listen directive. The new http-origin directive allows the CORS
allowed origins to be configured.

[1]: https://github.com/ircv3/ircv3-specifications/pull/342

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/conn.go

    r315 r323  
    22
    33import (
     4        "context"
    45        "fmt"
    56        "net"
     
    89
    910        "gopkg.in/irc.v3"
     11        "nhooyr.io/websocket"
    1012)
    1113
     
    1618        WriteMessage(*irc.Message) error
    1719        Close() error
     20        SetReadDeadline(time.Time) error
    1821        SetWriteDeadline(time.Time) error
    19         SetReadDeadline(time.Time) error
    2022}
    2123
    22 func netIRCConn(c net.Conn) ircConn {
     24func newNetIRCConn(c net.Conn) ircConn {
    2325        type netConn net.Conn
    2426        return struct {
     
    2628                netConn
    2729        }{irc.NewConn(c), c}
     30}
     31
     32type websocketIRCConn struct {
     33        conn                        *websocket.Conn
     34        readDeadline, writeDeadline time.Time
     35}
     36
     37func newWebsocketIRCConn(c *websocket.Conn) ircConn {
     38        return websocketIRCConn{conn: c}
     39}
     40
     41func (wic websocketIRCConn) ReadMessage() (*irc.Message, error) {
     42        ctx := context.Background()
     43        if !wic.readDeadline.IsZero() {
     44                var cancel context.CancelFunc
     45                ctx, cancel = context.WithDeadline(ctx, wic.readDeadline)
     46                defer cancel()
     47        }
     48        _, b, err := wic.conn.Read(ctx)
     49        if err != nil {
     50                return nil, err
     51        }
     52        return irc.ParseMessage(string(b))
     53}
     54
     55func (wic websocketIRCConn) WriteMessage(msg *irc.Message) error {
     56        b := []byte(msg.String())
     57        ctx := context.Background()
     58        if !wic.writeDeadline.IsZero() {
     59                var cancel context.CancelFunc
     60                ctx, cancel = context.WithDeadline(ctx, wic.writeDeadline)
     61                defer cancel()
     62        }
     63        return wic.conn.Write(ctx, websocket.MessageText, b)
     64}
     65
     66func (wic websocketIRCConn) Close() error {
     67        return wic.conn.Close(websocket.StatusNormalClosure, "")
     68}
     69
     70func (wic websocketIRCConn) SetReadDeadline(t time.Time) error {
     71        wic.readDeadline = t
     72        return nil
     73}
     74
     75func (wic websocketIRCConn) SetWriteDeadline(t time.Time) error {
     76        wic.writeDeadline = t
     77        return nil
    2878}
    2979
Note: See TracChangeset for help on using the changeset viewer.