Skip to content

feat: implement websocket.Accept in js (#373) #515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions accept.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build !js
// +build !js

package websocket

import (
Expand Down Expand Up @@ -167,7 +164,7 @@ func accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (_ *Con
b, _ := brw.Reader.Peek(brw.Reader.Buffered())
brw.Reader.Reset(io.MultiReader(bytes.NewReader(b), netConn))

return newConn(connConfig{
return &Conn{newConn(connConfig{
subprotocol: w.Header().Get("Sec-WebSocket-Protocol"),
rwc: netConn,
client: false,
Expand All @@ -178,7 +175,7 @@ func accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (_ *Con

br: brw.Reader,
bw: brw.Writer,
}), nil
})}, nil
}

func verifyClientRequest(w http.ResponseWriter, r *http.Request) (errCode int, _ error) {
Expand Down
19 changes: 8 additions & 11 deletions close.go → close_std.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build !js
// +build !js

package websocket

import (
Expand Down Expand Up @@ -97,7 +94,7 @@ func CloseStatus(err error) StatusCode {
//
// Close will unblock all goroutines interacting with the connection once
// complete.
func (c *Conn) Close(code StatusCode, reason string) (err error) {
func (c *StdConn) Close(code StatusCode, reason string) (err error) {
defer errd.Wrap(&err, "failed to close WebSocket")

if c.casClosing() {
Expand Down Expand Up @@ -130,7 +127,7 @@ func (c *Conn) Close(code StatusCode, reason string) (err error) {

// CloseNow closes the WebSocket connection without attempting a close handshake.
// Use when you do not want the overhead of the close handshake.
func (c *Conn) CloseNow() (err error) {
func (c *StdConn) CloseNow() (err error) {
defer errd.Wrap(&err, "failed to immediately close WebSocket")

if c.casClosing() {
Expand All @@ -155,7 +152,7 @@ func (c *Conn) CloseNow() (err error) {
return err
}

func (c *Conn) closeHandshake(code StatusCode, reason string) error {
func (c *StdConn) closeHandshake(code StatusCode, reason string) error {
err := c.writeClose(code, reason)
if err != nil {
return err
Expand All @@ -168,7 +165,7 @@ func (c *Conn) closeHandshake(code StatusCode, reason string) error {
return nil
}

func (c *Conn) writeClose(code StatusCode, reason string) error {
func (c *StdConn) writeClose(code StatusCode, reason string) error {
ce := CloseError{
Code: code,
Reason: reason,
Expand Down Expand Up @@ -196,7 +193,7 @@ func (c *Conn) writeClose(code StatusCode, reason string) error {
return nil
}

func (c *Conn) waitCloseHandshake() error {
func (c *StdConn) waitCloseHandshake() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

Expand Down Expand Up @@ -228,7 +225,7 @@ func (c *Conn) waitCloseHandshake() error {
}
}

func (c *Conn) waitGoroutines() error {
func (c *StdConn) waitGoroutines() error {
t := time.NewTimer(time.Second * 15)
defer t.Stop()

Expand Down Expand Up @@ -328,11 +325,11 @@ func (ce CloseError) bytesErr() ([]byte, error) {
return buf, nil
}

func (c *Conn) casClosing() bool {
func (c *StdConn) casClosing() bool {
return c.closing.Swap(true)
}

func (c *Conn) isClosed() bool {
func (c *StdConn) isClosed() bool {
select {
case <-c.closed:
return true
Expand Down
3 changes: 0 additions & 3 deletions compress.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build !js
// +build !js

package websocket

import (
Expand Down
Loading
Loading