socketio

package module
v0.0.0-...-d913432 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2014 License: BSD-3-Clause Imports: 11 Imported by: 0

README

socket.io

GoDoc Build Status

go-socket.io is the implement of socket.io in golang, which is a realtime application framework.

It compatible with latest implement of socket.io in node.js, and support room and namespace.

  • for compatible with socket.io 0.9.x, please use branch 0.9.x *

Install

Install the package with:

go get github.com/googollee/go-socket.io

Import it with:

import "github.com/googollee/go-socket.io"

and use socketio as the package name inside the code.

Example

Please check example folder for details.

package main

import (
	"log"
	"net/http"

	"github.com/googollee/go-socket.io"
)

func main() {
	server, err := socketio.NewServer(nil)
	if err != nil {
		log.Fatal(err)
	}
	server.On("connection", func(so socketio.Socket) {
		log.Println("on connection")
		so.Join("chat")
		so.On("chat message", func(msg string) {
			log.Println("emit:", so.Emit("chat message", msg))
			so.BroadcastTo("chat", "chat message", msg)
		})
		so.On("disconnection", func() {
			log.Println("on disconnect")
		})
	})
	server.On("error", func(so socketio.Socket, err error) {
		log.Println("error:", err)
	})

	http.Handle("/socket.io/", server)
	http.Handle("/", http.FileServer(http.Dir("./asset")))
	log.Println("Serving at localhost:5000...")
	log.Fatal(http.ListenAndServe(":5000", nil))
}

License

The 3-clause BSD License - see LICENSE for more details

Documentation

Overview

go-socket.io is the implement of socket.io in golang.

It is compatible with node.js implement.

Index

Constants

View Source
const Protocol = 4

Variables

This section is empty.

Functions

This section is empty.

Types

type Attachment

type Attachment struct {

	// Data is the ReadWriter of the attachment data.
	Data io.ReadWriter
	// contains filtered or unexported fields
}

Attachment is an attachment handler used in emit args. All attachments will send as binary in transport layer. When use attachment, make sure use as pointer.

For example:

type Arg struct {
    Title string `json:"title"`
    File *Attachment `json:"file"`
}

f, _ := os.Open("./some_file")
arg := Arg{
    Title: "some_file",
    File: &Attachment{
        Data: f,
    }
}

socket.Emit("send file", arg)
socket.On("get file", func(so Socket, arg Arg) {
    b, _ := ioutil.ReadAll(arg.File.Data)
})

func (Attachment) MarshalJSON

func (a Attachment) MarshalJSON() ([]byte, error)

func (*Attachment) UnmarshalJSON

func (a *Attachment) UnmarshalJSON(b []byte) error

type BroadcastAdaptor

type BroadcastAdaptor interface {

	// Join lets socket join the t room.
	Join(room string, socket Socket) error

	// Leave let socket leave the room.
	Leave(room string, socket Socket) error

	// Send will send the message with args to room. If ignore is not nil, it won't send to the socket ignore.
	Send(ignore Socket, room, message string, args []interface{}) error
}

BroadcastAdaptor is the adaptor to handle broadcast.

type Namespace

type Namespace interface {

	// Name returns the name of namespace.
	Name() string

	// Of returns the namespace with given name.
	Of(name string) Namespace

	// On registers the function f to handle message.
	On(message string, f interface{}) error
}

Namespace is the name space of socket.io handler.

type Server

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

Server is the server of socket.io.

func NewServer

func NewServer(transportNames []string) (*Server, error)

NewServer returns the server supported given transports. If transports is nil, server will use ["polling", "websocket"] as default.

func (*Server) BroadcastTo

func (s *Server) BroadcastTo(room, message string, args ...interface{})

Server level broadcasts function.

func (Server) Name

func (n Server) Name() string

func (Server) Of

func (n Server) Of(name string) Namespace

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles http request.

func (*Server) SetAdaptor

func (s *Server) SetAdaptor(adaptor BroadcastAdaptor)

SetAdaptor sets the adaptor of broadcast. Default is in-process broadcast implement.

func (*Server) SetAllowRequest

func (s *Server) SetAllowRequest(f func(*http.Request) error)

SetAllowRequest sets the middleware function when establish connection. If it return non-nil, connection won't be established. Default will allow all request.

func (*Server) SetAllowUpgrades

func (s *Server) SetAllowUpgrades(allow bool)

SetAllowUpgrades sets whether server allows transport upgrade. Default is true.

func (*Server) SetCookie

func (s *Server) SetCookie(prefix string)

SetCookie sets the name of cookie which used by engine.io. Default is "io".

func (*Server) SetPingInterval

func (s *Server) SetPingInterval(t time.Duration)

SetPingInterval sets the interval of ping. Default is 25s.

func (*Server) SetPingTimeout

func (s *Server) SetPingTimeout(t time.Duration)

SetPingTimeout sets the timeout of ping. When time out, server will close connection. Default is 60s.

type Socket

type Socket interface {

	// Id returns the session id of socket.
	Id() string

	// Rooms returns the rooms name joined now.
	Rooms() []string

	// Request returns the first http request when established connection.
	Request() *http.Request

	// On registers the function f to handle message.
	On(message string, f interface{}) error

	// Emit emits the message with given args.
	Emit(message string, args ...interface{}) error

	// Join joins the room.
	Join(room string) error

	// Leave leaves the room.
	Leave(room string) error

	// BroadcastTo broadcasts the message to the room with given args.
	BroadcastTo(room, message string, args ...interface{}) error
}

Socket is the socket object of socket.io.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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