GithubHelp home page GithubHelp logo

wcfylcf / chitchat Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ovenvan/chitchat

0.0 0.0 0.0 47 KB

A light-weight, high-concurrency Library Framework encapsulated GO socket.

License: MIT License

Go 100.00%

chitchat's Introduction

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:

  1. Tell it the address of the registration (listening);
  2. Tell it how to process the data after reading it;
  3. 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

  1. IP address(Addr:Port) being monitored,
  2. the delimiter of one sentence,
  3. the Read method,
  4. 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) 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

如何使用golang - chitchat

chitchat's People

Contributors

ovenvan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.