Ecosyste.ms: Packages

An open API service providing package, version and dependency metadata of many open source software ecosystems and registries.

Top 8.2% on proxy.golang.org

proxy.golang.org : github.com/raulsntos/gowebsockets

How to install A basic HTTPS server that uses this package under the /socket path: In the example we have a FileServer to serve static files like any other regular Go server and then we create the WebSocket. Then we have some event listeners and then we use http.Handle to assign the WebSocket handler to the /socket path like we would with an http.HandlerFunc, it works very similar to an HTTP server and you can use the handler however you use http.HandlerFunc in your server. The WebSocket.Handler is a function of type websocket.Handler, more information in the package https://godoc.org/golang.org/x/net/websocket. A basic javascript example to use with the WebSockets example: There are four types of event listeners: WebSocket.On, WebSocket.OnConnect, WebSocket.OnDisconnect and WebSocket.OnError. WebSocket.On takes a string (the name of the message, see the type gowebsockets.Message) and a gowebsockets.MessageListener (a method that takes a *gowebsockets.Client and a []byte). When you implement the gowebsockets.MessageListener you can use the Client which represents the connection to a client (a browser tab) to respond by sending messages (more on that below), and the []byte is the marshaled message content (gowebsockets.Message.Content), you would probably want to unmarshal the content in order to use it. WebSocket.OnConnect takes a gowebsockets.ConnectionListener which is a method that takes a *gowebsockets.Client, it is fired when a client connects to the server. WebSocket.OnDisconnect takes a gowebsockets.ConnectionListener which is a method that takes a *gowebsockets.Client, it is fired when a client disconnects from the server. WebSocket.OnError takes a gowebsockets.ErrorListener which is a method that takes an error, it is fired when there is an error like failing to parse the content of a received message because it's not a gowebsockets.Message JSON encoded string or any other errors with the WebSocket. In order to send and receive messages you'll have to use the gowebsockets.Message type which is a struct that contains a string Name and an interface{} Content. The Name will be used by the WebSocket.On event listener to fire the appropiate gowebsockets.MessageListener but you can always use the same name and handle every message in the same method. The Content contains the message data, you can assign data of any type and it will be encoded as JSON to send it, when you receive a message the WebSocket.On gives you access to the Content of the Message in []bytes so you'll have to unmarshal it using the encoding/json package, more information about the package in https://golang.org/pkg/encoding/json/. The event listeners give you access to a *gowebsockets.Client struct that contains the connection to a client. You can use the Client.ID to identify the client, it's a randomly generated UUID so it's unique. You can use the client's methods to send messages. See the type gowebsockets.Client for information about each method. By default, every Client joins a room with the same ID as the Client.ID, this room is called the personal room and the client cannot leave it. You can create rooms by using the gowebsockets.Client.Join method passing the ID of a room that doesn't exist. See the type type gowebsockets.Client for information about the methods Join, Leave and In and how they allow you to use rooms for sending messages.

Registry - Source - Documentation - JSON
purl: pkg:golang/github.com/raulsntos/gowebsockets
Keywords: golang, socket-io, sockets, websockets
License: MIT
Latest release: almost 8 years ago
First release: almost 8 years ago
Namespace: github.com/raulsntos
Stars: 2 on GitHub
Forks: 0 on GitHub
See more repository details: repos.ecosyste.ms
Last synced: 27 days ago

    Loading...
    Readme
    Loading...