golang php socket 框架,Golang 轻量级-高并发socket框架——chitchat

chitchat

-- import "github.com/ovenvan/chitchat"

What's this: This is a lightweight, support for high-concurrency communication Library framework, based on golang socket.

What's demo.go/demo_test.go: This is an simple Demo that tells you how to use this framework.

Usage

Whether for server or client, it only takes three steps to get it worked properly:

Tell it the address of the registration (listening);

Tell it how to process the data after reading it;

Correct handling Error notifications.

For Server:

type Server interface {

Listen() error

Cut() error

CloseRemote(string) error

RangeRemoteAddr() []string

GetLocalAddr() string

SetDeadLine(time.Duration, time.Duration)

ErrChan()

Write(interface{}) error

}

func NewServer

func NewServer(

ipaddrsocket string,

delim byte, readfunc ServerReadFunc, additional interface{}) Server

The method of registering the server with the framework. All you need to do is to tell the framework the

IP address(Addr:Port) being monitored,

the delimiter of one sentence,

the Read method,

and the additional information (could be nil).

if the delimiter is 0, then the framework will NOT send sentence to readfunc until EOF.

func Listen

func Listen() error

The method does not block the process, it asynchronously initiates the relevant goroutine to ensure that the framework works properly. At this point, Server is ready to start normally.

func Cut

func Cut() error

Terminates the listening service.

func CloseRemote

func CloseRemote(remoteAddr string) error

Closes a connection for a specific IP. If the connection to the IP does not exist, an error will be returned.

func RangeRemoteAddr

func RangeRemoteAddr() []string

Returns all the connections.

func ErrChan

func ErrChan()

Returns the channel allows consumers to receive errors.

...(some other funcs defined in server interface)

For Client:

type Client interface {

Dial() error

Close()

SetDeadLine(time.Duration)

ErrChan()

Write(interface{}) error

GetRemoteAddr() string

GetLocalAddr() string

}

func NewClient

func NewClient(

ipremotesocket string,

delim byte, readfunc ClientReadFunc, additional interface{}) Client {

Same as NewServer. however, the readfunc can be set nil.

func Dial() error

func Dial() error

Allows clients to connect to the remote server. You cannot manually select your own IP address for the time being, but you can obtain it through the GetLocalAddr () method. This method does not block the process.

func SetDeadLine(time.Duration)

func SetDeadLine(dDDL time.Duration)

This function works before Dial(). It specifies the maximum time for the connection.

How to write READ FUNC?

When the framework reads the separator, he hands over what he has previously read to the user-defined read func.

For Server and Client, their ReadFunc is the same, however with different names.

type ServerReadFunc func([]byte, ReadFuncer) error

type ClientReadFunc func([]byte, ReadFuncer) error

If you need to use additional parameters, you can get it through t.Addon ().

This parameter was already given (additional) when the Server (Client) was initialized. If the time is set to a null value, additional parameters cannot be obtained by the Addon method at this time.

Limitations of Write

The Write method encodes any transmitted data through JSON. You can decode it through the Unmarshal() function when you need it. The Write method automatically supplements the separator for you, so you don't need to artificially add delimiters.

However If the Write method provided by the framework does not suit you, you can replace it with the ' SetWriteFunc () ' function.

Demo

A simple heartbeat detection package.

For More info

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值