Skip to content

🐛 [Bug]: Can't close websocket connection #698

@mxgnus-de

Description

@mxgnus-de

Bug Description

I want to close a websocket connection but nothing happens (no error etc.).
The client remains connected and can also send messages.

How to Reproduce

Steps to reproduce the behavior:

  1. Run the code below
  2. Connect to the server with a websocket client

Expected Behavior

The server should close the connection after 5 seconds

Contrib package Version

websocket/v1.1.0

Code Snippet (optional)

package main

import (
	"log"
	"time"

	"github.com/gofiber/contrib/websocket"
	"github.com/gofiber/fiber/v2"
)

func main() {
	app := fiber.New()
	app.Use("/ws", middleware)
	app.Get("/ws", websocket.New(handler))
	log.Fatal(app.Listen(":8080"))
}

func middleware(c *fiber.Ctx) error {
	if websocket.IsWebSocketUpgrade(c) {
		return c.Next()
	}
	return fiber.ErrUpgradeRequired
}

func handler(conn *websocket.Conn) {
	go func() {
		time.Sleep(5 * time.Second)
		if err := conn.Close(); err != nil {
			log.Fatal(err)
		}
	}()

	for {
		_, msg, err := conn.ReadMessage()
		if err != nil {
			log.Println(err)
			return
		}
		log.Printf("msg: %s", msg)
	}
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my problem prior to opening this one.
  • I understand that improperly formatted bug reports may be closed without explanation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ☢️ BugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions