ws_server

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SecurePrefix   = "wss://"
	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>
`))

Functions

func Listen

func Listen(rawWsUrl string, tlsInfo *TlsInfo, handler http.HandlerFunc) error

helper for serving http and http servers

func WebsocketClientRoute

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

a test websocket client

Types

type BaseWebsocketService

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

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

func NewBaseWebsocketService

func NewBaseWebsocketService() BaseWebsocketService

func (BaseWebsocketService) Cancel

func (BaseWebsocketService) Context

func (w BaseWebsocketService) Context() context.Context

func (BaseWebsocketService) Hub

func (w BaseWebsocketService) Hub() *Hub

func (BaseWebsocketService) OnClose

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

func (BaseWebsocketService) OnConnect

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

func (BaseWebsocketService) OnConnecting

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

func (BaseWebsocketService) OnOpen

func (w BaseWebsocketService) OnOpen()

func (BaseWebsocketService) OnWsMessage

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

func (BaseWebsocketService) SendMessage

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

func (BaseWebsocketService) ServeHTTP

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

func (BaseWebsocketService) Upgrader

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

type TlsInfo

type TlsInfo struct{}

TODO

type WebSocketServerProtocol

type WebSocketServerProtocol interface {
	// context
	Context() context.Context
	// cancel func
	Cancel() context.CancelFunc
	// calls when a new connection is made
	OnConnecting(r *http.Request)
	// the client has connected
	OnConnect(r *http.Request)
	// connection is open
	OnOpen()
	// recieve a message, maps to OnMessage() in python version
	OnWsMessage(payload []byte, isBinary bool)
	// called when connection isclosed
	OnClose(wasClean bool, code int, reason string)
	// serve http interface. Must be implemented on child
	ServeHTTP(w http.ResponseWriter, r *http.Request)
	// get the upgrader
	Upgrader() websocket.Upgrader

	SendMessage(msg []byte) error
	// get hub
	Hub() *Hub
}

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

type WebsocketListener

type WebsocketListener struct{}

listener for websocket

Jump to

Keyboard shortcuts

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