server

package
v0.63.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SecurePrefix is used for secure websocket connections
	SecurePrefix = "wss://"
	// UnsecurePrefix is used for unsecured websocket connetions
	UnsecurePrefix = "ws://"
)

Variables

View Source
var WebsocketTemplate = template.Must(template.New("").Parse(`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>  
window.addEventListener("load", function(evt) {

    var output = document.getElementById("output");
    var input = document.getElementById("input");
    var ws;

    var print = function(message) {
        var d = document.createElement("div");
        d.textContent = message;
        output.appendChild(d);
    };

    document.getElementById("open").onclick = function(evt) {
        if (ws) {
            return false;
        }
        ws = new WebSocket("{{.}}");
        ws.onopen = function(evt) {
            print("OPEN");
        }
        ws.onclose = function(evt) {
            print("CLOSE");
            ws = null;
        }
        ws.onmessage = function(evt) {
            print("RESPONSE: " + evt.data);
        }
        ws.onerror = function(evt) {
            print("ERROR: " + evt.data);
        }
        return false;
    };

    document.getElementById("send").onclick = function(evt) {
        if (!ws) {
            return false;
        }
        print("SEND: " + input.value);
        ws.send(input.value);
        return false;
    };

    document.getElementById("close").onclick = function(evt) {
        if (!ws) {
            return false;
        }
        ws.close();
        return false;
    };

});
</script>
</head>
<body>
<table>
<tr><td valign="top" width="50%">
<p>Click "Open" to create a connection to the server, 
"Send" to send a message to the server and "Close" to close the connection. 
You can change the message and send multiple times.
<p>
<form>
<button id="open">Open</button>
<button id="close">Close</button>
<p><input id="input" type="text" value="Hello world!">
<button id="send">Send</button>
</form>
</td><td valign="top" width="50%">
<div id="output"></div>
</td></tr></table>
</body>
</html>
`))

WebsocketTemplate is a template for a websocket

Functions

func Listen

func Listen(rawWsURL string, tlsInfo *TLSInfo, handler http.HandlerFunc) error

Listen is helper for serving http and http servers

func ServeHTTP

ServeHTTP processes http request

func WebsocketClientRoute

func WebsocketClientRoute(host string, w http.ResponseWriter, r *http.Request)

WebsocketClientRoute handles http request

Types

type BaseWebsocketService

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

BaseWebsocketService you can wrap in a struct so you don't need to reimplement empty event listeners

func NewBaseWebsocketService

func NewBaseWebsocketService() BaseWebsocketService

NewBaseWebsocketService creates a BaseWebsocketService

func (BaseWebsocketService) Cancel

Cancel gets the context cancellation function implemented in a sub-struct

func (BaseWebsocketService) Context

func (w BaseWebsocketService) Context() context.Context

Context gets the context.Context object

func (BaseWebsocketService) Hub

func (w BaseWebsocketService) Hub() *Hub

Hub gets the Hub used to process websocket requests

func (BaseWebsocketService) OnClose

func (w BaseWebsocketService) OnClose(wasClean bool, code int, reason string)

OnClose is called after the websocket service is closed implemented in a sub-struct

func (BaseWebsocketService) OnConnect

func (w BaseWebsocketService) OnConnect(r *http.Request)

OnConnect is called after the websocket conntects implemented in a sub-struct

func (BaseWebsocketService) OnConnecting

func (w BaseWebsocketService) OnConnecting(r *http.Request)

OnConnecting is called as the websocket connects implemented in a sub-struct

func (BaseWebsocketService) OnOpen

func (w BaseWebsocketService) OnOpen()

OnOpen is called after a message is opened implemented in a sub-struct

func (BaseWebsocketService) OnWsMessage

func (w BaseWebsocketService) OnWsMessage(payload []byte, isBinary bool)

OnWsMessage processes a websocket message implemented in a sub-struct

func (BaseWebsocketService) SendMessage

func (w BaseWebsocketService) SendMessage(msg []byte) error

SendMessage sends a message to the client

func (BaseWebsocketService) ServeHTTP

func (w BaseWebsocketService) ServeHTTP(writer http.ResponseWriter, _ *http.Request)

ServeHTTP serves an http request implemented in a sub-struct

func (BaseWebsocketService) Upgrader

Upgrader is the websocket.Upgrader used to process ws requests implemented in a sub-struct

type Client

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

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

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() *Hub

NewHub creates a Hub

type TLSInfo

type TLSInfo struct{}

TLSInfo is a struct that contains ssl serving data

type WebSocketServerProtocol

type WebSocketServerProtocol interface {
	// Context gets the context.Context object
	Context() context.Context
	// Cancel gets the context cancellation function
	Cancel() context.CancelFunc
	// OnConnecting is called as the websocket connects
	OnConnecting(r *http.Request)
	// OnConnect is called after the websocket conntects
	OnConnect(r *http.Request)
	// OnOpen is called after a message is opened
	OnOpen()
	// OnWsMessage processes a websocket message
	OnWsMessage(payload []byte, isBinary bool)
	// OnClose is called after the websocket service is closed
	OnClose(wasClean bool, code int, reason string)
	// ServeHTTP serves an http request
	ServeHTTP(w http.ResponseWriter, r *http.Request)
	// Upgrader is the websocket.Upgrader used to process ws requests
	Upgrader() websocket.Upgrader
	// SendMessage sends a message to the client
	SendMessage(msg []byte) error
	// Hub gets the Hub used to process websocket requests
	Hub() *Hub
}

WebSocketServerProtocol attempts to emulate the twisted socket interface for usabilities sake and calls events on downstream nexuses

type WebsocketListener

type WebsocketListener struct{}

WebsocketListener is the listener for websocket

Jump to

Keyboard shortcuts

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