jolta

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

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

Go to latest
Published: Nov 22, 2022 License: Unlicense Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Jolta

Go Report Card License Documentation

This is an event-based networking library for Go, based on KCP and Smux.

Features

  • Secure: all of the data is encrypted by default, and there's no way to disable it.
  • Small latency: the KCP protocol is designed for small latencies.
  • Small: the entire library is ~650 lines of code.

Examples

(Server) Start a server

package main

import "github.com/zeozeozeo/jolta"

func main() {
    // server address, password, salt
    server := jolta.NewServer("127.0.0.1:7145", []byte("test password"), []byte("test salt"))

    // listen (this is blocking)
	if err := server.Listen(); err != nil {
		panic(err)
	}
}

(Client) Connect to a server

package main

import "github.com/zeozeozeo/jolta"

func main() {
    // server address, password and salt
    // of the server you want to connect to
    client := jolta.NewClient("127.0.0.1:7145", []byte("test password"), []byte("test salt"))

    // connect to the server (this is blocking)
    if err := client.Connect(); err != nil {
		panic(err)
	}
}

(Client) Send and recieve messages to/from other clients

func main() {
    // client := jolta.NewClient(...)

    client.OnConnect(func(client *jolta.Client) {
        // send a message to all connected
        // clients, the event name can be
        // any string
		client.SendAll("any event name", []byte("hello from client!"))

        // get all clients connected to
        // the server
        clients, err := client.GetClients()
        if err != nil || len(clients) == 0 {
            return
        }

        // send a message to some client
        // (clients are represented with
        // ID's, 1 being the client that
        // connected first)
        client.SendTo("any event name", clients[0], []byte("hi client!"))
	})

    // recieve messages from other clients
	client.On("any event name", func(client *jolta.Client, senderId uint32, data []byte) {
		// do anything you want...
        fmt.Printf("client %d says: %s\n", senderId, string(data))
	})

    // client.Connect()...
}

See more examples in internal/examples

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDisconnected error = errors.New("jolta client: disconnected")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	Address      string
	Password     []byte
	Salt         []byte
	DataShards   int // default: 10.
	ParityShards int // default: 3.
	KeyIter      int // default: 1024.
	KeyLen       int // default: 32.
	Events       []*Event
	Timeout      time.Duration // Timeout for accepting streams. 0 disables the timeout.
	// contains filtered or unexported fields
}

func NewClient

func NewClient(address string, password, salt []byte) *Client

NewClient creates a new client.

func (*Client) Connect

func (client *Client) Connect() error

Connect connects the client to the server.

func (*Client) Disconnect

func (client *Client) Disconnect()

Disconnect disconnects the client from the server.

func (*Client) GetClients

func (client *Client) GetClients() ([]uint32, error)

GetClients returns all client ID's that are connected to the server.

func (*Client) ID

func (client *Client) ID() uint32

Returns the client ID. An ID of 0 means that the client is not connected yet.

func (*Client) On

func (client *Client) On(eventName string, on func(client *Client, senderId uint32, data []byte))

On adds a hander to an event name. Empty event names will be called on any event.

func (*Client) OnConnect

func (client *Client) OnConnect(on func(client *Client))

OnDisconnect sets the function that is called when the client is connected.

func (*Client) OnDisconnect

func (client *Client) OnDisconnect(on func(client *Client))

OnDisconnect sets the function that is called before the client is disconnected.

func (*Client) SendAll

func (client *Client) SendAll(eventName string, data []byte) error

SendAll sends a message to all clients.

func (*Client) SendTo

func (client *Client) SendTo(eventName string, id uint32, data []byte) error

SendTo sends a message to a client with that ID.

type Connection

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

func (*Connection) Disconnect

func (conn *Connection) Disconnect()

Disconnect disconnects the client from the server.

func (*Connection) ID

func (conn *Connection) ID() uint32

ID returns the client ID.

func (*Connection) Send

func (conn *Connection) Send(eventName string, senderId uint32, data []byte) error

Send sends a message to the client.

type Event

type Event struct {
	Func func(client *Client, senderId uint32, data []byte)
	Name string
}

type Packet

type Packet uint8
const (
	PACKET_GETID      Packet = 0 // This packet is sent by the client when it's connected, the server responds with the client's ID.
	PACKET_SENDTO     Packet = 1 // The client tells the server to send a message to a specified user ID
	PACKET_SENDALL    Packet = 2 // The client tells the server to send a message to all clients
	PACKET_MESSAGE    Packet = 3 // The server tells the client that someone sent it a new message
	PACKET_GETCLIENTS Packet = 4 // The client asks the server to list all clients
)

type Server

type Server struct {
	Address      string
	Password     []byte
	Salt         []byte
	DataShards   int // default: 10
	ParityShards int // default: 3
	Connections  []*Connection
	KeyIter      int           // default: 1024
	KeyLen       int           // default: 32
	Timeout      time.Duration // Timeout for accepting streams. 0 disables the timeout.
	// contains filtered or unexported fields
}

func NewServer

func NewServer(address string, password, salt []byte) *Server

NewServer creates a new server context.

func (*Server) Listen

func (server *Server) Listen() error

Listen starts the server and listens for connections.

func (*Server) OnConnect

func (server *Server) OnConnect(on func(conn *Connection))

OnConnect sets the function that is called when a client is connected.

func (*Server) OnDisconnect

func (server *Server) OnDisconnect(on func(conn *Connection))

OnDisconnect sets the function that is called when a client is connected.

func (*Server) OnMessage

func (server *Server) OnMessage(on func(conn *Connection, eventName string, senderId uint32, data []byte))

OnMessage sets the function that is called when a client receives a message.

func (*Server) OnStart

func (server *Server) OnStart(on func(server *Server))

OnStart sets the function that is called when the server is started.

func (*Server) Stop

func (server *Server) Stop()

Stop disconnects all clients and stops the server.

Directories

Path Synopsis
internal
examples/simple command

Jump to

Keyboard shortcuts

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