anysocket
v0.4.29
Lapisan jaringan abstrak pada beberapa transport, klien/server agnostik dengan dukungan untuk E2EE
Ini masih dalam proses dan API dapat berubah.
AnySocket.Packer
)/dist/anysocket.browser.js
)** Transportasi http bersifat eksperimental
Info: Argumen dan respons RPC biner dikemas/dibongkar secara otomatis ( AnySocket.Packer.pack
/ AnySocket.Packer.unpack
).
Lihat tolok ukur
npm install -- save anysocket
atau
< script src =" /dist/anysocket.browser.js " > </ script >
atau (jika menggunakan AnySocketHTTP)
< script src =" @anysocket " > </ script >
Contoh berikut memulai server websocket pada port 3000.
const AnySocket = require ( "anysocket" ) ;
const server = new AnySocket ( ) ;
const PORT = 3000 ;
server . listen ( "ws" , PORT )
. then ( ( ) => {
console . log ( "Listening on port:" , PORT ) ;
} )
. catch ( ( err ) => {
console . error ( "Failed to start server:" , err ) ;
} ) ;
server . on ( "connected" , async ( peer ) => {
console . log ( "Connected" , peer . id ) ;
console . log ( await peer . getSyncedTime ( ) ) // { time: 1674671482107, rtt: 2, offset: 0 }
peer . send ( {
hello : "world"
} ) ;
} ) ;
server . on ( "message" , ( packet ) => {
console . log ( "From:" , packet . peer . id , "Message:" , packet . msg ) ;
} ) ;
server . on ( "disconnected" , ( peer , reason ) => {
console . log ( "Disconnected" , peer . id , "Reason:" , reason ) ;
} ) ;
Contoh berikut terhubung ke websocket pada port 3000
const AnySocket = require ( "anysocket" ) ;
const client = new AnySocket ( ) ;
const PORT = 3000 ;
client . connect ( "ws" , "127.0.0.1" , PORT )
. then ( ( ) => {
console . log ( "Connected to server" ) ;
} )
. catch ( ( err ) => {
console . error ( "Failed to connect to server:" , err ) ;
} ) ;
// after negotiating the AUTH packet, it will trigger the connect event
client . on ( "connected" , async ( peer ) => {
console . log ( "Connected" , peer . id ) ;
console . log ( await peer . getSyncedTime ( ) ) // { time: 1674671482107, rtt: 2, offset: 0 }
peer . send ( {
hello : "world"
} ) ;
} ) ;
client . on ( "message" , ( packet ) => {
console . log ( "From:" , packet . peer . id , "Message:" , packet . msg ) ;
} ) ;
client . on ( "disconnected" , ( peer , reason ) => {
console . log ( "Disconnected" , peer . id , "Reason:" , reason ) ;
} ) ;
Selengkapnya di folder examples
.
Lihat folder dokumentasi dan contoh
* Hal ini memerlukan perubahan dalam protokol, karena protokol mengasumsikan paket dikirim menggunakan koneksi yang andal dan teratur
MIT