socketio_client

package module
v0.0.0-...-83ee737 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2020 License: BSD-3-Clause Imports: 20 Imported by: 46

README

go-socket.io-client

go-socket.io-client is an client implementation of socket.io in golang, which is a realtime application framework.

It is compatible with latest implementation of socket.io in node.js, and supports namespace.

Install

Install the package with:

go get github.com/zhouhui8915/go-socket.io-client

Import it with:

import "github.com/zhouhui8915/go-socket.io-client"

and use socketio_client as the package name inside the code.

Example

Please check the example folder for details.

package main

import (
	"bufio"
	"github.com/zhouhui8915/go-socket.io-client"
	"log"
	"os"
)

func main() {

	opts := &socketio_client.Options{
		Transport: "websocket",
		Query:     make(map[string]string),
	}
	opts.Query["user"] = "user"
	opts.Query["pwd"] = "pass"
	uri := "http://192.168.1.70:9090/socket.io/"

	client, err := socketio_client.NewClient(uri, opts)
	if err != nil {
		log.Printf("NewClient error:%v\n", err)
		return
	}

	client.On("error", func() {
		log.Printf("on error\n")
	})
	client.On("connection", func() {
		log.Printf("on connect\n")
	})
	client.On("message", func(msg string) {
		log.Printf("on message:%v\n", msg)
	})
	client.On("disconnection", func() {
		log.Printf("on disconnect\n")
	})

	reader := bufio.NewReader(os.Stdin)
	for {
		data, _, _ := reader.ReadLine()
		command := string(data)
		client.Emit("message", command)
		log.Printf("send message:%v\n", command)
	}
}

License

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

Documentation

Index

Constants

View Source
const Protocol = 4

Variables

View Source
var InvalidError = errors.New("invalid transport")

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 Client

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

func NewClient

func NewClient(uri string, opts *Options) (client *Client, err error)

func (*Client) Emit

func (client *Client) Emit(message string, args ...interface{}) (err error)

func (*Client) On

func (client *Client) On(message string, f interface{}) (err error)

type MessageType

type MessageType message.MessageType

type Options

type Options struct {
	Transport string            //protocol name string,websocket polling...
	Query     map[string]string //url的附加的参数
	Header    map[string][]string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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