Panda is a library for event-based communications via WebSocket.
This is panda-client
written in TypeScript. You can use it in your modern Web Apps along with Panda
.
go get -u github.com/techerfan/panda
First you need to create a new App
.
In order to create a new App, you have this option to whether pass configuration or not. Configuration consists of:
ServerAddress
: It is the address to the server. The default is:8000
.WebSocketPath
: The path of Web Socket. The default is/ws
.CommunicationType
: You can choose the method of sending your data via Web Socket. The default isJSON
and unfortunately,XML
andBinary
are not implemented yet.DoNotShowLogs
: It is a boolean. If it istrue
, the module will not print logs and if it isfalse
, The logger will work and you will be able to see logs. The default isfalse
.LogsHeader
: It is astring
item. The logger will add it to the beginning of each log. The default isPanda
.AuthenticationHandler
: This handler validates client's connection. If it is nil, package will consider that authentication is not needed and let the client to establish the connection. It takes a token as input and returns a boolean in order to specify whether continue or not and a time that shows when the connection should be destroyed.TicketTokenExpirationHandler
: This handler decides what to do when a client's ticket is expired. If it is nil, there will be no default behavior.Logger
: You can use your own logger if it follows this interface.
AuthenticationHandler
, you need to generate a ticket by yourself and add it to the WebSocket URL as a query (e.g. http://localhost:8000/ws?ticket=MY_TICKET). This way, AuthenticationHandler
validate the ticket each time a client tries to connect to server (Do not forget you need to generate ticket yourself and validate it by implementing AuthenticationHandler
).
import "github.com/techerfan/panda"
app := panda.NewApp(panda.Config{
ServerAddress: ":8080",
WebSocketPath: "/ws",
CommunicationType: panda.JSON,
NotShowLogs: flase,
LogsHeader: "My App",
})
// or you can stick to the defaults and simply do this:
app := panda.NewApp()
Panda lets you know, by the NewConnection
method, whenever a client connects to the server:
app.NewConnection(func(client *panda.Client) {
// handle the client here...
})
OnMessage
: Listens to the messages which client sends to the server:
client.OnMessage(func (msg string) {
// do whatever you want with the message...
})
On
: Listens to the messages that are exchanged over a specified channel. You can decide whether send them to the clients or do something else:
client.On("chat_message", func(msg string) {
// do sth with the message...
})
Send
: To Send a message to the client:
client.Send("your message")
Publish
: To publish a message over a specified channel:
client.Publish("channel_name", "your message")
GetTicket
: Each client is authenticated via a ticket. You can get this ticket by this method:
var ticket string
ticket = client.GetTicket()
Destroy
: To destroy a client's connection.
err := client.Destroy()
GetClientID
: To get the client's ID.
var ClientID string = client.GetClientID()
Context
: To get the context of the client.
ctx := client.Context()
Licensed under the MIT License.