Golang 轻量级-高并发socket框架——chitchat
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() <-chan Errsocket 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() <-chan Errsocket
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() <-chan Errsocket 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) errortype 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
如何使用golang - chitchat
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~