()
| 80 | } |
| 81 | |
| 82 | func (c *Conn) init() { |
| 83 | c.closed = make(chan struct{}) |
| 84 | c.readSignal = make(chan struct{}, 1) |
| 85 | |
| 86 | c.msgReadLimit.Store(32768) |
| 87 | |
| 88 | c.releaseOnClose = c.ws.OnClose(func(e wsjs.CloseEvent) { |
| 89 | err := CloseError{ |
| 90 | Code: StatusCode(e.Code), |
| 91 | Reason: e.Reason, |
| 92 | } |
| 93 | // We do not know if we sent or received this close as |
| 94 | // its possible the browser triggered it without us |
| 95 | // explicitly sending it. |
| 96 | c.close(err, e.WasClean) |
| 97 | |
| 98 | c.releaseOnClose() |
| 99 | c.releaseOnError() |
| 100 | c.releaseOnMessage() |
| 101 | }) |
| 102 | |
| 103 | c.releaseOnError = c.ws.OnError(func(v js.Value) { |
| 104 | c.setCloseErr(errors.New(v.Get("message").String())) |
| 105 | c.closeWithInternal() |
| 106 | }) |
| 107 | |
| 108 | c.releaseOnMessage = c.ws.OnMessage(func(e wsjs.MessageEvent) { |
| 109 | c.readBufMu.Lock() |
| 110 | defer c.readBufMu.Unlock() |
| 111 | |
| 112 | c.readBuf = append(c.readBuf, e) |
| 113 | |
| 114 | // Lets the read goroutine know there is definitely something in readBuf. |
| 115 | select { |
| 116 | case c.readSignal <- struct{}{}: |
| 117 | default: |
| 118 | } |
| 119 | }) |
| 120 | |
| 121 | runtime.SetFinalizer(c, func(c *Conn) { |
| 122 | c.setCloseErr(errors.New("connection garbage collected")) |
| 123 | c.closeWithInternal() |
| 124 | }) |
| 125 | } |
| 126 | |
| 127 | func (c *Conn) closeWithInternal() { |
| 128 | c.Close(StatusInternalError, "something went wrong") |
no test coverage detected