ws

package
v0.0.0-...-c979b4e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2023 License: BSD-2-Clause, MPL-2.0 Imports: 8 Imported by: 0

README

使用示例

ws_serviceinit中含有了HManagerrun. 详见: 此处

ws "github.com/IanVzs/Snowflakes/services/ws_service"

func Chat(c *gin.Context) {
	var hub *ws.Hub
	var ok bool
	chatID := c.Query("chat_id")
	if chatID != "" {
		conn, error := (&websocket.Upgrader{
			CheckOrigin: func(r *http.Request) bool { return true }}).Upgrade(c.Writer, c.Request, nil)
		if error != nil {
			http.NotFound(c.Writer, c.Request)
			return
		}
		if hub, ok = ws.HManager.Hubs[chatID]; !ok {
			hub = ws.NewHub(chatID)
			logging.Infof("first in chatID: %s", chatID)
		} else {
			logging.Infof("join in chatID: %s", chatID)
		}
		ws.ServeWs(hub, conn)
	} else {
		return
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var HManager = HubManager{
	Hubs: make(map[string]*Hub),
	// contains filtered or unexported fields
}
View Source
var Manager = EchoClientManager{
	Broadcast:   make(chan []byte),
	Register:    make(chan *EchoClient),
	Unregister:  make(chan *EchoClient),
	EchoClients: make(map[*EchoClient]bool),
}

Manager define a echo server manager

Functions

func ServeWs

func ServeWs(hub *Hub, conn *websocket.Conn)

serveWs handles websocket requests from the peer.

func ServeWs(hub *Hub, w http.ResponseWriter, r *http.Request) {
	if !websocket.IsWebSocketUpgrade(r) {
		log.Println("/ws not websocket request")
	}
	conn, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		log.Println(err)
		return
	}

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a middleman between the websocket connection and the hub.

type EchoClient

type EchoClient struct {
	ID     string
	Socket *websocket.Conn
	Send   chan []byte
}

EchoClient is a websocket client

func (*EchoClient) Read

func (c *EchoClient) Read()

func (*EchoClient) Write

func (c *EchoClient) Write()

type EchoClientManager

type EchoClientManager struct {
	EchoClients map[*EchoClient]bool
	Broadcast   chan []byte
	Register    chan *EchoClient
	Unregister  chan *EchoClient
}

EchoClientManager is a websocket manager

func (*EchoClientManager) Send

func (manager *EchoClientManager) Send(message []byte, ignore *EchoClient)

Send is to send ws message to ws client

func (*EchoClientManager) Start

func (manager *EchoClientManager) Start()

Start is to start a ws server

type Hub

type Hub struct {
	// contains filtered or unexported fields
}

Hub maintains the set of active clients and broadcasts messages to the clients.

func NewHub

func NewHub(hubID string) *Hub

type HubManager

type HubManager struct {
	Hubs map[string]*Hub
	// contains filtered or unexported fields
}

type Message

type Message struct {
	Sender    string `json:"sender,omitempty"`
	Recipient string `json:"recipient,omitempty"`
	Content   string `json:"content,omitempty"`
}

Message is an object for websocket message which is mapped to json type

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL