@@ -118,128 +121,4 @@ export async function perfectNegotiation(onMessage,sendMessage,polite){
}
};
});
}
/**
* Custom socket class to pass strings to and from a server.
*
* Originally, the idea was to let this pass json data instead, but since json is difficult to work with in java I've stuck with normal strings for now.
*/
exportclassCustomSocket{
/**
* @callback MessageCallback
* @param {string} message
* @param {()=>void} remove removes this callback again
* @returns {void}
*/
/**
* @callback ErrorCallback
* @param {Event} error
* @param {()=>void} remove removes this callback again
* Returns a Promise that resolves to the next message received that passen the given filter, or `null` if no such message is received in the specified timeframe.
* If an error occurs before, the promise will instead be rejected with that.
* @param {(message:string)=>boolean} filter
* @param {number} time time to wait before just returning `null`, in seconds. Defaults to 30. If the given number is smaller than or equal to 0, it gets treated as `Infinity`.
* @return {Promise<string>}
*/
asyncnext(filter=null,time=30){
returnnewPromise((resolve,reject)=>{
this.onMessage((message,remove)=>{
if (!filter||filter(message)){
resolve(message);
remove();
}
});
this.onError((error,remove)=>{
reject(error);
remove();
});
if (time>0){
setTimeout(()=>resolve(null),time*1000);
}
});
}
/**
* Registers a callback to be called whenever there's an error.
* @param {ErrorCallback} callback
*/
onError(callback){
this._onErrorCallbacks.push(callback);
}
/**
* Removes a callback registered using `onError(callback)`.