Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

WebSocket API wrapper for creating and managing WebSocket connections

License

NotificationsYou must be signed in to change notification settings

mudssrali/wsgrok

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WebSocket API wrapper for creating and managing WebSocket connections

JavaScript Style Guide

Install

Install with npm or yarn via

yarn add wsgrok

or

npm i wsgrok

API

typeEvent="connect"|"disconnect"|"message"|"verify"interfaceWSgrok{url:stringready:booleanws?:WebSocketpingInterval?:numberpending:Map<string,{resolve:(a:any)=>anyreject:(a:any)=>any}>messageTimeout:numberconnectionVerified:booleanverify():voidconnect():Promise<void>on(event:Event,f:Function):voidonNext(event:Event,f:Function):voidcleanup():voidping():voidsend(message:any,timeout?:number):Promise<any>}

Usage

  • UseWSgrok with events

    importWSgrokfrom'wsgrok'constwsgrok=newWSgrok("wss://example.com/ws")// on connected to hostwsgrok.on('connect',()=>console.log("connected to a host"))// on disconnected from a hostwsgrok.on('disconnect',()=>console.log("disconnected from a host"))// on receiving a new message from hostwsgrok.on('message',(message:any)=>console.log("new message",message))// on verify a connection to hostwsgrok.on('verify',()=>console.log("connection is verified"))
  • Make a request to host usingwsgrok.Send() withPromise

    importWSgrokfrom'wsgrok'exportconstinvokeService=()=>{constwsgrok=newWSgrok("wss://www.example.com/ws")wsgrok.send({type:"INVOKE_SERVICE",clientType:"XNGINX"payload:{initiatorId:"xxxx-xxxx-xxxx"reqToken:"xxxx-xxxx-xxxx"}}).then(response=>{// do your work here}).catch(error=>{// do your work here})}

    Or with async arrow style

    importWSgrokfrom'wsgrok'exportconstinvokeService=aysnc()=>{const wsgrok=newWSgrok("wss://www.example.com/ws")try{constresponse=awaitwsgrok.send({type:"INVOKE_SERVICE",clientType:"XNGINX",payload:{initiatorId:"xxxx-xxxx-xxxx"reqToken:"xxxx-xxxx-xxxx"}})// handle server response here}catch(error){// handle server error here}}

About

WebSocket API wrapper for creating and managing WebSocket connections

Topics

Resources

License

Stars

Watchers

Forks


[8]ページ先頭

©2009-2025 Movatter.jp