Web-UDP是一個庫,用於在節點/瀏覽器環境中建立不可靠的數據通道。該項目提供一個小型穩定的API的關鍵目標,任何人都可以用來使用網絡上的實時數據。
該庫目前是在無序和不可靠的RTCDATACHANNELS之上實現的。由於WEBRTC是一種依賴關係,因此包含基於Websoket的信號服務器,以促進客戶端之間的連接。客戶/服務器連接可藉助WRTC軟件包。
Signal < T > . subscribe ( subscriber : T => any )
Signal < T > . unsubscribe ( subscriber : T => any )
Client ( options ?: { url ?: string } )
Client . connect ( to ?: string = "__MASTER__" , options ?: {
binaryType ?: "arraybuffer" | "blob" ,
maxRetransmits ?: number ,
maxPacketLifeTime ?: number ,
metadata ?: any ,
UNSAFE_ordered ?: boolean
} ) : Promise < Connection >
Client . route ( ) : Promise < string >
Client . connections : Signal < Connection >
Connection . send ( message : any ) : void
Connection . close ( ) : void
Connection . closed : Signal
Connection . errors : Signal < { err : string } >
Connection . messages : Signal < any >
Connection . metadata : any
// Node
Server ( { server : http . Server , keepAlivePeriod ?: number = 30000 } )
Server . client ( ) : Client
Server . connections : Signal < Connection > npm i @web-udp/client
npm i @web-udp/server以下是Ping服務器的一個簡單示例:
// client.js
import { Client } from "@web-udp/client"
async function main ( ) {
const udp = new Client ( )
const connection = await udp . connect ( )
connection . send ( "ping" )
connection . messages . subscribe ( console . log )
} // server.js
const server = require ( "http" ) . createServer ( )
const { Server } = require ( "@web-udp/server" )
const udp = new Server ( { server } )
udp . connections . subscribe ( connection => {
connection . messages . subscribe ( message => {
if ( message === "ping" ) {
connection . send ( "pong" )
}
} )
connection . closed . subscribe ( ( ) => console . log ( "A connection closed." ) )
connection . errors . subscribe ( err => console . log ( err ) )
} )
server . listen ( 8000 )Client.connect中的metadata選項用於建立連接後立即發送任意握手數據。建立新連接後,遠程客戶端可以在連接對象的metadata屬性上訪問此數據,而無需訂閱遠程客戶端的消息。握手元數據通過安全的RTCDataChannel傳輸,使其成為諸如密碼之類的敏感數據的良好候選者。
在下面的示例中,服務器在訂閱客戶端消息之前處理身份驗證:
// client.js
const connection = await udp . connect ( {
metadata : {
credentials : {
username : "foo" ,
password : "bar" ,
} ,
} ,
} ) // server.js
udp . connections . subscribe ( connection => {
let user
try {
user = await fakeAuth . login ( connection . metadata . credentials )
} catch ( err ) {
// Authentication failed, close connection immediately.
connection . send ( fakeProtocol . loginFailure ( ) )
connection . close ( )
return
}
// The user authenticated successfully.
connection . send ( fakeProtocol . loginSuccess ( user ) )
connection . messages . subscribe ( ... )
} ) web-udp還支持點對點交流。下面的示例演示了兩個在同一瀏覽器選項卡中連接的客戶端:
<!-- index.html -->
< script src =" /node_modules/@web-udp/client/dist/index.js " > </ script >
< script src =" client.js " > </ script > // client.js
async function main ( ) {
const left = new Udp . Client ( )
const right = new Udp . Client ( )
const route = await left . route ( )
const connection = await right . connect ( route )
left . connections . subscribe ( connection =>
connection . messages . subscribe ( console . log ) ,
)
connection . send ( "HELLO" )
} // server.js
const server = require ( "http" ) . createServer ( )
const { Server } = require ( "@web-udp/server" )
Server ( { server } )
server . listen ( 8000 ) Client.connect (可選)選擇RTCDATACHANNEL maxPacketLifeTime和maxRetransmits選項。這些選項可用於啟用無序可靠的數據頻道。
Websocket用作信號傳輸。在建立Datachannel以轉發其關閉事件(例如關閉瀏覽器選項卡時)以終止懸掛web-udp連接後,該插座將保持打開狀態。在主機帶有連接超時的情況下,定期發送keetalive信號,以保持插座打開。可以通過服務器的keepAlivePeriod選項配置發送keepalive信號的周期。
版權2023 Eric McDaniel
特此免費授予獲得此軟件副本和相關文檔文件副本(“軟件”)的任何人,以無限制處理該軟件,包括無限制的使用權,複製,複製,修改,合併,合併,發布,分發,分發,分發,訂婚,和/或允許軟件的副本,並允許對以下條件提供以下條件,以下是以下條件。
上述版權通知和此許可通知應包含在軟件的所有副本或大量部分中。
該軟件是“原樣”提供的,沒有任何形式的明示或暗示保證,包括但不限於適銷性,特定目的的適用性和非侵權的保證。在任何情況下,作者或版權持有人都不應對任何索賠,損害賠償或其他責任責任,無論是在合同,侵權的訴訟中還是其他責任,是由軟件,使用或與軟件中的使用或其他交易有關的。