Web-UUDPは、ノード/ブラウザー環境で信頼できないデータチャネルを確立するために使用されるライブラリです。このプロジェクトの主要な目標は、誰でも使用してWeb上のリアルタイムデータを使用するために使用できる小さく安定したAPIを提供するという重要な目標です。
ライブラリは現在、順序付けられていない信頼できないrtcdatachannelsの上に抽象化として実装されています。 WeBRTCは依存関係であるため、クライアント間の接続を容易にするために、WebSocketベースのシグナリングサーバーがパッケージに含まれています。 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 、ピアツーピアコミュニケーションもサポートしています。以下の例は、同じブラウザタブで接続されている2つのクライアントを示しています。
<!-- 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接続を終了するために、緊密なイベント(たとえば、ブラウザタブが閉じられている場合)を転送した後に開いた状態に保たれます。接続タイムアウトのホストの場合、ソケットを開いたままにするために、キープライブ信号が定期的に送信されます。 KeepAlive信号が送信される期間は、サーバーのkeepAlivePeriodオプションを介して構成できます。
著作権2023エリック・マクダニエル
このソフトウェアと関連するドキュメントファイル(「ソフトウェア」)のコピーを入手して、制限なしにソフトウェアを扱うために、このソフトウェアを制限する権利を含め、ソフトウェアのコピーをコピー、変更、公開、配布、販売する、ソフトウェアのコピーを許可する人を許可する人を許可することを含めて、許可が無料で許可されます。
上記の著作権通知とこの許可通知は、ソフトウェアのすべてのコピーまたはかなりの部分に含まれるものとします。
このソフトウェアは、商品性、特定の目的への適合性、および非侵害の保証を含むがこれらに限定されない、明示的または黙示的なものを保証することなく、「現状のまま」提供されます。いかなる場合でも、著者または著作権所有者は、契約、不法行為、またはその他の訴訟、ソフトウェアまたはソフトウェアの使用またはその他の取引に関連する、またはその他の契約、またはその他の請求、またはその他の責任について責任を負いません。