socketio

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2013 License: MIT Imports: 7 Imported by: 1

README

Socket.IO

Package socketio implements a client for SocketIO protocol in Go language as specified in socket.io-spec

Usage

package main

import (
	"fmt"
	"github.com/oguzbilgic/socketio"
)

func main() {
	// Open a new client connection to the given socket.io server
	// Connect to the given channel on the socket.io server
	socket, err := socketio.DialAndConnect("socketio-server.com:80", "/channel", "key=value")
	if err != nil {
		panic(err)
	}

	for {
		// Receive socketio.Message from the server
		msg, err := socket.Receive()
		if err != nil {
			panic(err)
		}

		fmt.Printf("Type: %v, ID: '%s', Endpoint: '%s', Data: '%s' \n", msg.Type, msg.ID, msg.Endpoint, msg.Data)
	}
}

Documentation

http://godoc.org/github.com/oguzbilgic/socketio

License

The MIT License (MIT)

Documentation

Overview

Package socketio implements a client for SocketIO protocol as specified in https://github.com/LearnBoost/socket.io-spec

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Endpoint

type Endpoint struct {
	Path  string
	Query string
}

Endpoint is a socket.io endpoint.

func NewEndpoint

func NewEndpoint(path, query string) *Endpoint

NewEndpoint returns a new Endpoint with the given path and query.

func ParseEndpoint

func ParseEndpoint(rawEndpoint string) *Endpoint

ParseEndpoint parses the given rawn endpoint into an Endpoint.

func (Endpoint) String

func (e Endpoint) String() string

String returns the string representation of the endpoint.

type Message

type Message struct {
	Type     int
	ID       string
	Endpoint *Endpoint
	Data     string
}

func NewACK

func NewACK(data string) *Message

func NewConnect

func NewConnect(endpoint *Endpoint) *Message

NewConnect returns a new connect Message for the given endpoint.

func NewDisconnect

func NewDisconnect() *Message

NewDisconnect returns a new disconnect Message.

func NewError

func NewError(endpoint *Endpoint, reason string, advice string) *Message

NewError returns a new error Message for the given endpoint with a reason and an advice.

func NewEvent

func NewEvent(endpoint *Endpoint, name string, args string) *Message

func NewHeartbeat

func NewHeartbeat() *Message

NewHeartbeat returns a new heartbeat Message.

func NewJSONMessage

func NewJSONMessage(endpoint *Endpoint, data string) *Message

func NewMessageMsg

func NewMessageMsg(endpoint *Endpoint, msg string) *Message

func NewNoop

func NewNoop() *Message

NewNoop returns a new no-op Message.

func ParseMessage

func ParseMessage(rawMsg string) (*Message, error)

ParseMessages parses the given raw message in to Message.

func (Message) String

func (m Message) String() string

String returns the string represenation of the Message.

type Session

type Session struct {
	ID                 string
	HeartbeatTimeout   time.Duration
	ConnectionTimeout  time.Duration
	SupportedProtocols []string
}

Session holds the configuration variables received from the socket.io server.

func NewSession

func NewSession(url string) (*Session, error)

NewSession receives the configuraiton variables from the socket.io server.

func (*Session) SupportProtocol

func (session *Session) SupportProtocol(protocol string) bool

SupportProtocol checks if the given protocol is supported by the socket.io server.

type Socket

type Socket struct {
	URL       string
	Session   *Session
	Transport Transport
}

func Dial

func Dial(url string) (*Socket, error)

Dial opens a new client connection to the socket.io server using one of the implemented and supported Transports.

Example
socket, err := Dial("localhost:12345")
if err != nil {
	panic(err)
}

for {
	msg, err := socket.Receive()
	if err != nil {
		panic(err)
	}

	fmt.Println(msg)
}
Output:

func DialAndConnect

func DialAndConnect(url string, channel string, query string) (*Socket, error)

Dial opens a new client connection to the socket.io server then connects to the given channel.

Example
socket, err := DialAndConnect("localhost:12345", "/channel", "key=value")
if err != nil {
	panic(err)
}

for {
	msg, err := socket.Receive()
	if err != nil {
		panic(err)
	}

	fmt.Println(msg)
}
Output:

func (*Socket) Receive

func (socket *Socket) Receive() (*Message, error)

Receive receives the raw message from the underlying transport and converts it to the Message type.

func (*Socket) Send

func (socket *Socket) Send(msg *Message) error

Send sends the given Message to the socket.io server using it's underlying transport.

type Transport

type Transport interface {
	Send(string) error
	Receive() (string, error)
}

Transport is an interface for sending and receiving raw messages from the socket.io server.

func NewTransport

func NewTransport(session *Session, url string) (Transport, error)

NewTransport returns an implemented transport which is also supported by the socket.io server.

type WSTransport

type WSTransport struct {
	Conn *websocket.Conn
}

WSTransport implements Transport interface for websocket protocol.

func NewWSTransport

func NewWSTransport(session *Session, url string) (*WSTransport, error)

func (*WSTransport) Receive

func (wsTransport *WSTransport) Receive() (string, error)

func (*WSTransport) Send

func (wsTransport *WSTransport) Send(rawMsg string) error

Jump to

Keyboard shortcuts

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