wstext

package module
v0.0.0-...-348056f Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2014 License: BSD-2-Clause Imports: 2 Imported by: 0

README

go.wstext

go.wstext is a small wrapper package for gorilla/websocket. It adds .WriteText(msg) and .ReadText() methods to websocket.Conn. This is useful if you send a lot of "plain-text" commands back and forth.. Otherwise: don't bother and use the WriteMessage() and ReadMessage().

Example usage:

import (
	"github.com/GeertJohan/go.wstext"
	"github.com/gorilla/websocket"
	"net/http"
)

func handleWebsocket(w http.ResponseWriter, r *http.Request) {
	conn, err := websocket.Upgrade(w, r, nil, 1024, 1024)
	if err != nil {
		// handle error
	}

	textconn := wstext.Conn{conn}

	text, err := textconn.ReadText()
	if err != nil {
		if err == wstext.ErrWrongType {
			// a non-text message was received
		}
		// handle error
	}

	err = textconn.WriteText("Hello, world")
	if err != nil {
		// handle error
	}

	// All original methods on `websocket.Conn` are still available on `wstext.Conn`.
	textconn.WriteJson(someData)
}

License

This project is licensed under a Simplified BSD license. Please read the LICENSE file.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrWrongType = errors.New("websocket: received wrong type")

Functions

This section is empty.

Types

type Conn

type Conn struct {
	*websocket.Conn
}

func (Conn) ReadText

func (c Conn) ReadText() (msg string, err error)

ReadText reads a new text message from the websocket. When retrieved message was not of type Text: wstext.ErrWrongType is returned.

func (Conn) WriteText

func (c Conn) WriteText(message string) error

WriteText writes given string with messageType TextMessage Is actually a shorthand for (*Conn).WriteMessage(websocket.TextMessage, []byte(message))

Jump to

Keyboard shortcuts

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