protoevent
v1.0.0 Release
ProtoEvent adalah pustaka penanganan koneksi TCP berbasis peristiwa di Golang. Sederhana dan tidak terlalu banyak mengeksploitasi fungsi dasar pembuatan koneksi protokol baru. ProtoEvent mengimplementasikan kembali antarmuka net.Listener
dan net.Conn
untuk memperluas kemampuan menangkap berbagai peristiwa yang terjadi dalam komunikasi jaringan.
Cukup menyakitkan ketika Anda harus menjaga basis kode aplikasi jaringan sederhana Anda yang tidak terorganisir dengan baik berakhir dengan menghasilkan lebih banyak kode spaghetti. Setidaknya, kini kami dapat mengatur logika aplikasi berdasarkan peristiwa jaringan tertentu.
go get -u https://github.com/parinpan/protoevent
Aplikasi sisi server:
package main
import (
"encoding/json"
"fmt"
"net"
"github.com/parinpan/protoevent"
)
type Message struct {
From string `json:"from"`
}
func main () {
servant , event , err := protoevent . CreateServant ( "tcp" , "0.0.0.0:8089" )
if nil != err {
panic ( err )
}
event . OnConnectionError ( func ( err error ) {
})
event . OnConnectionAccepted ( func ( conn net. Conn ) {
fmt . Println ( "Accepting new connection: " , conn . RemoteAddr ())
})
event . OnConnectionClosed ( func ( conn net. Conn ) {
})
event . OnReceiveMessageError ( func ( conn net. Conn , err error ) {
})
event . OnMessageReceived ( func ( conn net. Conn , message [] byte , rawMessage [] byte ) {
fmt . Println ( "Received a message: " , string ( message ))
var msg Message
_ = json . Unmarshal ( message , & msg )
// send a message back to client
sayHiMessage := fmt . Sprint ( "Hi " , msg . From , ". Welcome to ProtoEvent!" )
conn . Write ([] byte ( sayHiMessage ))
})
event . OnSendMessageError ( func ( conn net. Conn , message [] byte , err error ) {
})
event . OnMessageSent ( func ( conn net. Conn , message [] byte ) {
fmt . Println ( "Sent a message: " , string ( message ))
})
servant . SetDefaultReadSize ( 4096 ) // set default read size per chunk in bytes
servant . Serve ()
}
Aplikasi sisi klien:
package main
import (
"fmt"
"net"
"github.com/parinpan/protoevent"
)
func main () {
agent , event := protoevent . CreateAgent ( "tcp" , "0.0.0.0:8089" )
agent . SetDefaultReadSize ( 4096 ) // set default read size per chunk in bytes
event . OnConnectionError ( func ( err error ) {
})
event . OnConnectionAccepted ( func ( conn net. Conn ) {
fmt . Println ( "Accepting new connection: " , conn . RemoteAddr ())
})
event . OnConnectionClosed ( func ( conn net. Conn ) {
})
event . OnReceiveMessageError ( func ( conn net. Conn , err error ) {
})
event . OnMessageReceived ( func ( conn net. Conn , message [] byte , rawMessage [] byte ) {
fmt . Println ( "Received a message: " , string ( message ))
})
event . OnSendMessageError ( func ( conn net. Conn , message [] byte , err error ) {
})
event . OnMessageSent ( func ( conn net. Conn , message [] byte ) {
fmt . Println ( "Sent a message: " , string ( message ))
})
// trigger a message to get connected with the server at first
err := agent . Run ( func ( conn net. Conn ) error {
_ , err := conn . Write ([] byte ( `{"from": "AgentV1"}` ))
return err
})
if nil != err {
panic ( err )
}
}
Jika Anda memiliki pertanyaan. Anda dapat menghubungi saya di: