wss

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2022 License: MIT Imports: 5 Imported by: 3

README

a socket wrapper for TCP/UDP/WEB socket

1. tcp socket example

1.1 tcp socket client

package main

import (
	"github.com/civet148/gotools/log"
	"github.com/civet148/gotools/wss"
	_ "github.com/civet148/gotools/wss/tcpsock" //required (register tcp socket instance)
	"time"
)

const (
	TCP_DATA_PING = "ping"
	TCP_DATA_PONG = "pong"
)

func main() {
	var url = "tcp://127.0.0.1:6666"
	client(url)
}

func client(strUrl string) {
	c := wss.NewClient()
	if err := c.Connect(strUrl); err != nil {
		log.Errorf(err.Error())
		return
	}

	for {
		if _, err := c.Send([]byte(TCP_DATA_PING)); err != nil {
			log.Errorf(err.Error())
			break
		}

		if data, from, err := c.Recv(len(TCP_DATA_PONG)); err != nil {
			log.Error(err.Error())
			break
		} else {
			log.Infof("tcp client received data [%s] length [%v] from [%v]", string(data), len(data), from)
		}

		time.Sleep(3 * time.Second)
	}
}

1.2 tcp socket server

package main

import (
	"github.com/civet148/gotools/log"
	"github.com/civet148/gotools/wss"
	_ "github.com/civet148/gotools/wss/tcpsock" //required (register tcp socket instance)
)

const (
	TCP_DATA_PING = "ping"
	TCP_DATA_PONG = "pong"
)

type Server struct {
	service *wss.SocketServer
}

func main() {

	var url = "tcp://0.0.0.0:6666"
	server(url)

	var c = make(chan bool, 1)
	<-c //block main go routine
}

func server(strUrl string) {

	var server Server
	server.service = wss.NewServer(strUrl)
	if err := server.service.Listen(&server); err != nil {
		log.Errorf(err.Error())
		return
	}
}

func (s *Server) OnAccept(c *wss.SocketClient) {
	log.Infof("connection accepted [%v]", c.GetRemoteAddr())
}

func (s *Server) OnReceive(c *wss.SocketClient, data []byte, length int, from string) {
	log.Infof("tcp server received data [%s] length [%v] from [%v]", data, length, from)
	if string(data) == TCP_DATA_PING {
		if _, err := c.Send([]byte(TCP_DATA_PONG)); err != nil {
			log.Errorf(err.Error())
		}
	}
}

func (s *Server) OnClose(c *wss.SocketClient) {
	log.Infof("connection [%v] closed", c.GetRemoteAddr())
}

2. udp socket example

2.1 udp socket client

package main

import (
	"github.com/civet148/gotools/log"
	"github.com/civet148/gotools/wss"
	_ "github.com/civet148/gotools/wss/udpsock" //required (register udp socket instance)
	"time"
)

const (
	UDP_CLIENT_URL  = "udp://127.0.0.1:6664"
	UDP_SERVER_ADDR = "udp://127.0.0.1:6665"
)

func main() {
	client(UDP_CLIENT_URL)
}

const (
	UDP_DATA_PING = "ping"
	UDP_DATA_PONG = "pong"
)

func client(strUrl string) (err error) {
	c := wss.NewClient()
	if err = c.Listen(strUrl); err != nil {
		log.Errorf(err.Error())
		return
	}
	for {
		log.Debugf("local address [%v] remote address [%v]", c.GetLocalAddr(), c.GetRemoteAddr())
		if _, err := c.Send([]byte(UDP_DATA_PING), UDP_SERVER_ADDR); err != nil {
			log.Errorf(err.Error())
			break
		}

		if data, from, err := c.Recv(-1); err != nil {
			log.Error(err.Error())
			break
		} else {
			log.Infof("udp client received data [%s] length [%v] from [%v]", string(data), len(data), from)
		}
		time.Sleep(3 * time.Second)
	}
	return
}

2.2 udp socket server

package main

import (
	"github.com/civet148/gotools/log"
	"github.com/civet148/gotools/wss"
	_ "github.com/civet148/gotools/wss/udpsock" //required (register udp socket instance)
)

const (
	UDP_DATA_PING  = "ping"
	UDP_DATA_PONG  = "pong"
	UDP_SERVER_URL = "udp://0.0.0.0:6665"
)

type Server struct {
	service *wss.SocketServer
}

func main() {

	server(UDP_SERVER_URL)

	var c = make(chan bool, 1)
	<-c //block main go routine
}

func server(strUrl string) {

	var server Server
	server.service = wss.NewServer(strUrl)
	if err := server.service.Listen(&server); err != nil {
		log.Errorf(err.Error())
		return
	}
}

func (s *Server) OnAccept(c *wss.SocketClient) {
	//log.Infof("connection accepted [%v]", c.GetRemoteAddr())
}

func (s *Server) OnReceive(c *wss.SocketClient, data []byte, length int, from string) {
	log.Infof("udp server received data [%s] length [%v] from [%v] ", data, length, from)
	if string(data) == UDP_DATA_PING {
		if _, err := c.Send([]byte(UDP_DATA_PONG), from); err != nil {
			log.Errorf(err.Error())
		}
	}
}

func (s *Server) OnClose(c *wss.SocketClient) {
	//log.Infof("connection [%v] closed", c.GetRemoteAddr())
}

3. web socket example

3.1 web socket client

package main

import (
	"github.com/civet148/gotools/log"
	"github.com/civet148/gotools/wss"
	_ "github.com/civet148/gotools/wss/websock"
	"time"
)

const (
	WEBSOCKET_DATA_PING = "ping"
	WEBSOCKET_DATA_PONG = "pong"
)

func main() {
	client("ws://127.0.0.1:6668/websocket")
}

func client(strUrl string) (err error) {
	c := wss.NewClient()
	if err = c.Connect(strUrl); err != nil {
		log.Errorf(err.Error())
		return
	}

	for {
		var data []byte
		var from string

		if _, err := c.Send([]byte(WEBSOCKET_DATA_PING)); err != nil {
			log.Errorf(err.Error())
			break
		}

		if data, from, err = c.Recv(-1); err != nil {
			log.Errorf(err.Error())
			break
		}
		log.Infof("web socket client received data [%s] length [%v] from [%v]", data, len(data), from)
		time.Sleep(3 * time.Second)
	}
	return
}

3.2 web socket server

package main

import (
	"github.com/civet148/gotools/log"
	"github.com/civet148/gotools/wss"
	_ "github.com/civet148/gotools/wss/websock"
)

const (
	WEBSOCKET_DATA_PING = "ping"
	WEBSOCKET_DATA_PONG = "pong"
)

type Server struct {
	service *wss.SocketServer
}

func main() {
	server("ws://0.0.0.0:6668/websocket")
	var c = make(chan bool, 1)
	<-c //block main go routine
}

func server(strUrl string) {
	var server Server
	server.service = wss.NewServer(strUrl)
	server.service.Listen(&server)
}

func (s *Server) OnAccept(c *wss.SocketClient) {
	log.Infof("connection accepted [%v]", c.GetRemoteAddr())
}

func (s *Server) OnReceive(c *wss.SocketClient, data []byte, length int, from string) {
	log.Infof("web socket server received data [%s] length [%v] from [%v]", data, length, from)
	if string(data) == WEBSOCKET_DATA_PING {
		if _, err := c.Send([]byte(WEBSOCKET_DATA_PONG)); err != nil {
			log.Errorf(err.Error())
		}
	}
}

func (s *Server) OnClose(c *wss.SocketClient) {
	log.Infof("connection [%v] closed", c.GetRemoteAddr())
}

4. unix socket example

4.1 unix socket client

package main

import (
	"github.com/civet148/gotools/log"
	"github.com/civet148/gotools/wss"
	_ "github.com/civet148/gotools/wss/unixsock" //required (register tcp socket instance)
	"time"
)

const (
	UNIX_DATA_PING = "ping"
	UNIX_DATA_PONG = "pong"
)

func main() {
	var url = "unix:///tmp/unix.sock"
	client(url)
}

func client(strUrl string) {
	c := wss.NewClient()
	if err := c.Connect(strUrl); err != nil {
		log.Errorf(err.Error())
		return
	}

	for {
		if _, err := c.Send([]byte(UNIX_DATA_PING)); err != nil {
			log.Errorf(err.Error())
			break
		}

		if data, from, err := c.Recv(len(UNIX_DATA_PONG)); err != nil {
			log.Error(err.Error())
			break
		} else {
			log.Infof("unix client received data [%s] length [%v] from [%v]", string(data), len(data), from)
		}

		time.Sleep(3 * time.Second)
	}
}

4.2 unix socket server

package main

import (
	"github.com/civet148/gotools/log"
	"github.com/civet148/gotools/wss"
	_ "github.com/civet148/gotools/wss/unixsock" //required (register tcp socket instance)
)

const (
	UNIX_DATA_PING = "ping"
	UNIX_DATA_PONG = "pong"
)

type Server struct {
	service *wss.SocketServer
}

func main() {

	var url = "unix:///tmp/unix.sock"
	server(url)

	var c = make(chan bool, 1)
	<-c //block main go routine
}

func server(strUrl string) {

	var server Server
	server.service = wss.NewServer(strUrl)
	if err := server.service.Listen(&server); err != nil {
		log.Errorf(err.Error())
		return
	}
}

func (s *Server) OnAccept(c *wss.SocketClient) {
	log.Infof("connection accepted [%v]", c.GetRemoteAddr())
}

func (s *Server) OnReceive(c *wss.SocketClient, data []byte, length int, from string) {
	log.Infof("unix server received data [%s] length [%v] from [%v]", data, length, from)
	if string(data) == UNIX_DATA_PING {
		if _, err := c.Send([]byte(UNIX_DATA_PONG)); err != nil {
			log.Errorf(err.Error())
		}
	}
}

func (s *Server) OnClose(c *wss.SocketClient) {
	log.Infof("connection [%v] closed", c.GetRemoteAddr())
}

Documentation

Index

Constants

View Source
const (
	URL_SCHEME_TCP  = "tcp"
	URL_SCHEME_TCP4 = "tcp4"
	URL_SCHEME_TCP6 = "tcp6"
	URL_SCHEME_UDP  = "udp"
	URL_SCHEME_UDP4 = "udp4"
	URL_SCHEME_UDP6 = "udp6"
	URL_SCHEME_WS   = "ws"
	URL_SCHEME_WSS  = "wss"
	URL_SCHEME_UNIX = "unix"
)
View Source
const (
	NETWORK_TCP   = "tcp"
	NETWORK_TCPv4 = "tcp4"
	NETWORK_TCPv6 = "tcp6"
	NETWORK_UDP   = "udp"
	NETWORK_UDPv4 = "udp4"
	NETWORK_UDPv6 = "udp6"
	NETWORK_UNIX  = "unix"
)
View Source
const (
	WSS_TLS_CERT = "cert"
	WSS_TLS_KEY  = "key"
)
View Source
const (
	PACK_FRAGMENT_MAX = 1500
)

Variables

This section is empty.

Functions

func Register

func Register(sockType SocketType, inst SocketInstance) (err error)

func SetLogDebug

func SetLogDebug(enable bool)

func SetLogFile

func SetLogFile(strPath string)

Types

type Socket

type Socket interface {
	Listen() (err error)                                   // bind and listen on address and port
	Accept() Socket                                        // accept connection...
	Connect() (err error)                                  // for tcp/web socket
	Send(data []byte, to ...string) (n int, err error)     // send to...
	Recv(length int) (data []byte, from string, err error) // receive from... if length > 0, will receive the bytes specified.
	Close() (err error)                                    // close socket
	GetLocalAddr() string                                  // get socket local address
	GetRemoteAddr() string                                 // get socket remote address
	GetSocketType() SocketType                             //get socket type
}

type SocketClient

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

func NewClient

func NewClient() *SocketClient

func (*SocketClient) Close

func (w *SocketClient) Close() (err error)

func (*SocketClient) Connect

func (w *SocketClient) Connect(url string) (err error)

IPv4 => tcp://127.0.0.1:6666 [tcp4://127.0.0.1:6666] WebSocket => ws://127.0.0.1:6668 [wss://127.0.0.1:6668]

func (*SocketClient) GetLocalAddr

func (w *SocketClient) GetLocalAddr() (addr string)

func (*SocketClient) GetRemoteAddr

func (w *SocketClient) GetRemoteAddr() (addr string)

func (*SocketClient) IsClosed

func (w *SocketClient) IsClosed() bool

func (*SocketClient) Listen

func (w *SocketClient) Listen(url string) (err error)

only for UDP

func (*SocketClient) Recv

func (w *SocketClient) Recv(length int) (data []byte, from string, err error)

func (*SocketClient) Send

func (w *SocketClient) Send(data []byte, to ...string) (n int, err error)

type SocketHandler

type SocketHandler interface {
	OnAccept(c *SocketClient)
	OnReceive(c *SocketClient, data []byte, length int, from string)
	OnClose(c *SocketClient)
}

type SocketInstance

type SocketInstance func(ui *parser.UrlInfo) Socket

type SocketServer

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

func NewServer

func NewServer(url string) *SocketServer

func (*SocketServer) Close

func (w *SocketServer) Close()

func (*SocketServer) CloseClient

func (w *SocketServer) CloseClient(client *SocketClient) (err error)

func (*SocketServer) GetClientAll

func (w *SocketServer) GetClientAll() (clients []*SocketClient)

func (*SocketServer) GetClientCount

func (w *SocketServer) GetClientCount() int

func (*SocketServer) Listen

func (w *SocketServer) Listen(handler SocketHandler) (err error)

TCP => tcp://127.0.0.1:6666 UDP => udp://127.0.0.1:6667 WebSocket => ws://127.0.0.1:6668/ wss://127.0.0.1:6668/websocket?cert=cert.pem&key=key.pem

func (*SocketServer) Send

func (w *SocketServer) Send(client *SocketClient, data []byte, to ...string) (n int, err error)

type SocketType

type SocketType int
const (
	SocketType_TCP  SocketType = 1
	SocketType_WEB  SocketType = 2
	SocketType_UDP  SocketType = 3
	SocketType_UNIX SocketType = 4
)

func (SocketType) GoString

func (s SocketType) GoString() string

func (SocketType) String

func (s SocketType) String() string

Jump to

Keyboard shortcuts

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