gows

package module
v0.0.0-...-1383e3e Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2018 License: MIT Imports: 11 Imported by: 0

README

gows

a simple implement of websocket written in go

Install

go get gitee.com/cuberl/gows

Usage

import (
	"fmt"
	"io"

	"gitee.com/cuberl/gows"
)

func main() {
	gows.New("localhost", 8091, func(conn *gows.Conn) {
		conn.Ping(30, func(conn *gows.Conn) {
			fmt.Println("ping timeout.")
		})
		for {
			data, err := conn.Read()
			if err != nil {
				if err == io.EOF {
					fmt.Println("Connection Close.")
				} else {
					fmt.Println(err)
				}
				break
			}
			fmt.Fprintf(conn, "hello, %s\n", string(data))
		}
	}).Start()
}

Documentation

Overview

Example
gows.New("localhost", 8091, func(conn *gows.Conn) {
	// ping the client per 30 secs.
	conn.Ping(30, func(conn *gows.Conn) {
		fmt.Println("ping timeout.")
	})
	for {
		data, err := conn.Read()
		if err != nil {
			if err == io.EOF {
				fmt.Println("Connection Close.")
			}
			break
		}
		fmt.Fprintf(conn, "hello, %s\n", string(data))
	}
}).Start()
Output:

Index

Examples

Constants

View Source
const (
	OpcodeExtraData       = 0x00
	OpcodeTextData        = 0x01
	OpcodeBinaryData      = 0x02
	OpcodeConnectionClose = 0x08
	OpcodePing            = 0x09
	OpcodePong            = 0x0A
)
View Source
const ConnUUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"

Variables

This section is empty.

Functions

This section is empty.

Types

type AcceptCallback

type AcceptCallback func(conn *Conn)

type Conn

type Conn struct {
	Addr net.Addr
	// contains filtered or unexported fields
}

func (*Conn) CancelPing

func (c *Conn) CancelPing()

Cancel to send keepalive to client.

func (*Conn) Ping

func (c *Conn) Ping(interval int, errCallback func(*Conn))

Start to send keepalive data to client, It will ping client per (interval) seconds, the errCallback will be call when pong client didn't arrive in (interval) seconds, you can call CancelPing to cancel it.

func (*Conn) Read

func (c *Conn) Read() ([]byte, error)

Read a frame from client, It will return io.EOF when the connection is closed, It will be block if there is not data yet.

func (*Conn) Write

func (c *Conn) Write(b []byte) (n int, err error)

This function implements the interface io.Writer, So you can write data to websocket like a file

type Server

type Server struct {
	Host string
	Port int
	// contains filtered or unexported fields
}

func New

func New(Host string, Port int, callback AcceptCallback) *Server

It will listen the addr (Host):(Port). the callback will be call when a connection is accepted.

func (*Server) Start

func (s *Server) Start()

Start to listening the port you specified

Jump to

Keyboard shortcuts

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