tcp_server

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

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

Go to latest
Published: Jul 9, 2020 License: MIT Imports: 4 Imported by: 0

README

Build Status

TCPServer

Package tcp_server created to help build TCP servers faster.

Install package
go get -u github.com/firstrow/tcp_server
Usage:

NOTICE: OnNewMessage callback will receive new message only if it's ending with \n

package main

import "github.com/firstrow/tcp_server"

func main() {
	server := tcp_server.New("localhost:9999")

	server.OnNewClient(func(c *tcp_server.Client) {
		// new client connected
		// lets send some message
		c.Send("Hello")
	})
	server.OnNewMessage(func(c *tcp_server.Client, message string) {
		// new message received
	})
	server.OnClientConnectionClosed(func(c *tcp_server.Client, err error) {
		// connection with client lost
	})

	server.Listen()
}

Contributing

To hack on this project:

  1. Install as usual (go get -u github.com/firstrow/tcp_server)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Ensure everything works and the tests pass (go test)
  4. Commit your changes (git commit -am 'Add some feature')

Contribute upstream:

  1. Fork it on GitHub
  2. Add your remote (git remote add fork git@github.com:firstrow/tcp_server.git)
  3. Push to the branch (git push fork my-new-feature)
  4. Create a new Pull Request on GitHub

Notice: Always use the original import path by installing with go get.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client holds info about connection

func (*Client) Close

func (c *Client) Close() error

func (*Client) Conn

func (c *Client) Conn() net.Conn

func (*Client) Send

func (c *Client) Send(message string) error

Send text message to client

func (*Client) SendBytes

func (c *Client) SendBytes(b []byte) error

Send bytes to client

type Server

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

TCP Server

func New

func New(address string) *Server

Creates new tcp Server instance

func NewWithTLS

func NewWithTLS(address string, certFile string, keyFile string) *Server

func (*Server) Listen

func (s *Server) Listen()

Listen starts network Server

func (*Server) OnClientConnectionClosed

func (s *Server) OnClientConnectionClosed(callback func(c *Client, err error))

Called right after connection closed

func (*Server) OnNewClient

func (s *Server) OnNewClient(callback func(c *Client))

Called right after Server starts listening new client

func (*Server) OnNewMessage

func (s *Server) OnNewMessage(callback func(c *Client, message string))

Called when Client receives new message

Jump to

Keyboard shortcuts

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