sio

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2019 License: MIT Imports: 12 Imported by: 0

README

Socket.IO

Build Status

Unofficial server-side Socket.IO in Golang.

Example

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/jjeffcaii/socket.io"
)

func main() {
	server := sio.NewBuilder().Build()
	nsp := server.Of("/")
	nsp.OnConnect(func(socket sio.Socket) {
		socket.On("news", func(msg sio.Message) {
			fmt.Println("[news]:", msg)
		})
		socket.Emit("hello", "你好,世界!")
		socket.Join("chats")
		socket.To("chats").Emit("hello", "Hello World!")
	})
	http.HandleFunc(sio.DefaultPath, server.Router())
	log.Fatalln(http.ListenAndServe(":3000", nil))
}

Documents

Please see https://godoc.org/github.com/jjeffcaii/socket.io.

Documentation

Overview

Package sio is a server-side Socket.IO in Golang.

Basic Example:

var server = sio.NewBuilder().Build()
var nsp = server.Of("/")
nsp.OnConnect(func(socket sio.Socket) {
	socket.On("news", func(msg sio.Message) {
		fmt.Println("[news]:", msg.Any())
	})
	socket.Emit("hello", "你好,世界!")
})
http.HandleFunc(sio.DefaultPath, server.Router())
log.Fatalln(http.ListenAndServe(":3000", nil))

Index

Constants

View Source
const DefaultPath = "/socket.io/"

DefaultPath '/socket.io/' is the default http handler path.

Variables

This section is empty.

Functions

This section is empty.

Types

type Emitter added in v0.0.2

type Emitter interface {
	// Emit emits an event to the socket identified by the string name.
	Emit(event string, first interface{}, others ...interface{}) error
}

Emitter emits event to sockets.

type Handshake

type Handshake struct {
	Headers http.Header
	Address string
	XDomain bool
	URL     string
	Query   url.Values
}

Handshake is the object used when negociating the handshake.

type Message

type Message []byte

Message is an alias of event message.

func (Message) Any added in v0.0.2

func (s Message) Any() interface{}

Any parse bytes to anything.

func (Message) Parse added in v0.0.2

func (s Message) Parse(dest interface{}) error

Parse parse bytes as json.

func (Message) String added in v0.0.2

func (s Message) String() string

type Namespace

type Namespace interface {
	// ID returns the pathname of namespace.
	ID() string
	// OnConnect register handler for socket created in namespace.
	OnConnect(callback func(socket Socket))
	// GetSockets returns all of sockets in current namespace.
	GetSockets() Sockets
	// To targets a room when emitting.
	To(room string) Emitter
	// In targets a room when emitting.
	In(room string) Emitter
}

Namespace represents a pool of sockets connected under a given scope identified by a pathname (eg: /chat).

type Server

type Server interface {
	// Of initializes and retrieves the given Namespace by its pathname identifier nsp.
	Of(nsp string) Namespace
	// Router returns a http handler.
	Router() func(http.ResponseWriter, *http.Request)
	// GetSockets returns all sockets of server.
	GetSockets() Sockets
}

Server is socket.io server. You can use ServerBuilder to create it simply.

type ServerBuilder

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

ServerBuilder can be used to build a new server.

func NewBuilder

func NewBuilder() *ServerBuilder

NewBuilder returns a builder for server.

func (*ServerBuilder) Build

func (p *ServerBuilder) Build() Server

Build returns a new server.

func (*ServerBuilder) ForceCheckProtocol

func (p *ServerBuilder) ForceCheckProtocol() *ServerBuilder

ForceCheckProtocol force protocol check for query param EIO.

func (*ServerBuilder) SetAllowRequest

func (p *ServerBuilder) SetAllowRequest(validator func(*http.Request) error) *ServerBuilder

SetAllowRequest set a function that receives a given request, and can decide whether to continue or not.

func (*ServerBuilder) SetAllowUpgrades

func (p *ServerBuilder) SetAllowUpgrades(enable bool) *ServerBuilder

SetAllowUpgrades define whether to allow transport upgrades. (default allow upgrades)

func (*ServerBuilder) SetCookie

func (p *ServerBuilder) SetCookie(enable bool) *ServerBuilder

SetCookie can control enable/disable of cookie.

func (*ServerBuilder) SetCookieHTTPOnly

func (p *ServerBuilder) SetCookieHTTPOnly(httpOnly bool) *ServerBuilder

SetCookieHTTPOnly if set true HttpOnly io cookie cannot be accessed by client-side APIs, such as JavaScript. (true) This option has no effect if cookie or cookiePath is set to false.

func (*ServerBuilder) SetCookiePath

func (p *ServerBuilder) SetCookiePath(path string) *ServerBuilder

SetCookiePath define the path of cookie.

func (*ServerBuilder) SetGenerateID

func (p *ServerBuilder) SetGenerateID(gen func(uint32) string) *ServerBuilder

SetGenerateID define the method of creating SocketID.

func (*ServerBuilder) SetLoggerError

func (p *ServerBuilder) SetLoggerError(logger func(format string, v ...interface{})) *ServerBuilder

SetLoggerError set logger for ERROR

func (*ServerBuilder) SetLoggerInfo

func (p *ServerBuilder) SetLoggerInfo(logger func(format string, v ...interface{})) *ServerBuilder

SetLoggerInfo set logger for INFO

func (*ServerBuilder) SetLoggerWarn

func (p *ServerBuilder) SetLoggerWarn(logger func(format string, v ...interface{})) *ServerBuilder

SetLoggerWarn set logger for WARN

func (*ServerBuilder) SetPath

func (p *ServerBuilder) SetPath(path string) *ServerBuilder

SetPath define the http router path for Engine.

func (*ServerBuilder) SetPingInterval

func (p *ServerBuilder) SetPingInterval(interval time.Duration) *ServerBuilder

SetPingInterval define ping time interval in millseconds for client. (default is 60 seconds)

func (*ServerBuilder) SetPingTimeout

func (p *ServerBuilder) SetPingTimeout(timeout time.Duration) *ServerBuilder

SetPingTimeout define ping timeout in millseconds for client. (default is 25 seconds)

func (*ServerBuilder) SetTransports

func (p *ServerBuilder) SetTransports(transports ...eio.TransportType) *ServerBuilder

SetTransports define transport types allow.

type Socket

type Socket interface {
	// ID returns session ID of socket.
	ID() string
	// Namespace returns Namespace of current socket.
	Namespace() Namespace
	// Handshake returns Handshake of current socket.
	Handshake() *Handshake
	// Emit emits an event to the socket identified by the string name.
	Emit(event string, first interface{}, others ...interface{}) error
	// On register a handler of event identified by the string event.
	On(event string, callback func(msg Message)) error
	// OnError register a handler of error happend.
	OnError(callback func(error)) error
	// OnClose register a handler of socket closed.
	OnClose(callback func(reason string)) error
	// Close close current socket.
	Close()
	// To return an emitter to all room members exclude yourself or another socket.
	To(room string) Emitter
	// In return an emitter to all room members or another socket.
	In(room string) Emitter
	// Join joins a room.
	Join(room string) Socket
	// Leave leaves a room.
	Leave(room string) Socket
	// Leave leaves all the rooms that we've joined.
	LeaveAll() Socket
	// Properties returns properties map for current socket.
	Properties() *sync.Map
}

Socket is the fundamental class for interacting with browser clients. A Socket belongs to a certain Namespace (by default /) and uses an underlying Client to communicate.

type Sockets

type Sockets map[string]Socket

Sockets is an alias of socket map.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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