Categorygithub.com/hertz-contrib/websocket
modulepackage
0.1.0
Repository: https://github.com/hertz-contrib/websocket.git
Documentation: pkg.go.dev

# README

Hertz-WebSocket(This is a community driven project)

This repo is forked from Gorilla WebSocket and adapted to Hertz.

How to use

package main

import (
	"context"
	"log"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/hertz-contrib/websocket"
)

var upgrader = websocket.HertzUpgrader{} // use default options

func echo(_ context.Context, c *app.RequestContext) {
	err := upgrader.Upgrade(c, func(conn *websocket.Conn) {
		for {
			mt, message, err := conn.ReadMessage()
			if err != nil {
				log.Println("read:", err)
				break
			}
			log.Printf("recv: %s", message)
			err = conn.WriteMessage(mt, message)
			if err != nil {
				log.Println("write:", err)
				break
			}
		}
	})
	if err != nil {
		log.Print("upgrade:", err)
		return
	}
}


func main() {
	h := server.Default(server.WithHostPorts(addr))
	// https://github.com/cloudwego/hertz/issues/121
	h.NoHijackConnPool = true
	h.GET("/echo", echo)
	h.Spin()
}

More info

See examples

# Packages

No description provided by the author

# Functions

FormatCloseMessage formats closeCode and text as a WebSocket close message.
IsCloseError returns boolean indicating whether the error is a *CloseError with one of the specified codes.
IsUnexpectedCloseError returns boolean indicating whether the error is a *CloseError with a code not in the list of expected codes.
NewPreparedMessage returns an initialized PreparedMessage.

# Constants

BinaryMessage denotes a binary data message.
Close codes defined in RFC 6455, section 11.7.
Close codes defined in RFC 6455, section 11.7.
Close codes defined in RFC 6455, section 11.7.
Close codes defined in RFC 6455, section 11.7.
Close codes defined in RFC 6455, section 11.7.
CloseMessage denotes a close control message.
Close codes defined in RFC 6455, section 11.7.
Close codes defined in RFC 6455, section 11.7.
Close codes defined in RFC 6455, section 11.7.
Close codes defined in RFC 6455, section 11.7.
Close codes defined in RFC 6455, section 11.7.
Close codes defined in RFC 6455, section 11.7.
Close codes defined in RFC 6455, section 11.7.
Close codes defined in RFC 6455, section 11.7.
Close codes defined in RFC 6455, section 11.7.
PingMessage denotes a ping control message.
PongMessage denotes a pong control message.
TextMessage denotes a text data message.

# Variables

ErrCloseSent is returned when the application writes a message to the connection after sending a close message.
ErrReadLimit is returned when reading a message that is larger than the read limit set for the connection.

# Structs

CloseError represents a close message.
The Conn type represents a WebSocket connection.
HandshakeError describes an error with the handshake from the peer.
HertzUpgrader specifies parameters for upgrading an HTTP connection to a WebSocket connection.
PreparedMessage caches on the wire representations of a message payload.

# Interfaces

BufferPool represents a pool of buffers.

# Type aliases

HertzHandler receives a websocket connection after the handshake has been completed.