Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Flow } from "./async"
- export type WebSocketConnectFunction = () => void
- export type WebSocketMessageFunction = (data : any) => void
- export type WebSocketErrorFunction = (error: any) => void
- export type WebSocketDisconnectFunction = () => void
- export class Socket {
- private _connect : WebSocketConnectFunction = function() {}
- private _message : WebSocketMessageFunction = function() {}
- private _error : WebSocketErrorFunction = function() {}
- private _disconnect : WebSocketDisconnectFunction = function() {}
- private _connected : boolean = false
- private _flow : Flow = undefined
- private _socket : WebSocket = undefined
- /**
- * creates a new web socket connection
- * @param {SocketOptions} options the web socket options.
- * @returns {Socket}
- */
- constructor(private endpoint: string) {
- this._flow = new Flow()
- this._socket = new WebSocket(endpoint)
- this._socket.onopen = () => this.handle_connect ()
- this._socket.onmessage = (data) => this.handle_message (data)
- this._socket.onerror = (error) => this.handle_error (error)
- this._socket.onclose = () => this.handle_disconnect()
- }
- /**
- * subscribes to the socket connect event.
- * @param {WebSocketConnectFunction} func the listener function.
- * @returns {void}
- */
- public connect (func: WebSocketConnectFunction): void { this._connect = func }
- /**
- * subscribes to the socket message event.
- * @param {WebSocketMessageFunction} func the listener function.
- * @returns {void}
- */
- public message (func: WebSocketMessageFunction): void { this._message = func }
- /**
- * subscribes to the socket error event.
- * @param {WebSocketErrorFunction} func the listener function.
- * @returns {void}
- */
- public error (func: WebSocketErrorFunction): void { this._error = func }
- /**
- * subscribes to the socket disconnect event.
- * @param {WebSocketDisconnectFunction} func the listener function.
- * @returns {void}
- */
- public disconnect (func: WebSocketDisconnectFunction): void { this._disconnect = func }
- /**
- * sends a message to the server.
- * @param {any} data the data to send.
- * @returns {Promise<any>}
- */
- public send (data: any): Promise<any> {
- return this._flow.run(() => this._socket.send(JSON.stringify(data)))
- }
- /**
- * handles the connect event.
- * @returns {void}
- */
- private handle_connect(): void {
- this._connect()
- this._connected = true
- this._flow.resume()
- }
- /**
- * handles incoming messages
- * @param {any} data the data being received.
- * @returns {void}
- */
- private handle_message(data: MessageEvent): void {
- try {
- const message = JSON.parse(data.data)
- this._message(message)
- } catch(error) {
- this.handle_error(error)
- }
- }
- /**
- * handles error events on this socket.
- * @param {any} error the error.
- * @returns {void}
- */
- private handle_error(error: any): void {
- this._error(error)
- }
- /**
- * handles the socket disconnect event.
- * @returns {void}
- */
- private handle_disconnect(): void {
- this._disconnect()
- }
- }
- export type Address = { type: "address", id?: string, address?: string }
- export type Offer = { type: "offer", id?: string, from: string, to: string, data: any }
- export type Answer = { type: "answer", id?: string, from: string, to: string, data: any }
- export type Candidate = { type: "candidate", id?: string, from: string, to: string, data: any }
- export type Message = Address | Offer | Answer | Candidate
- export type OfferFunction = (offer : Offer) => void
- export type AnswerFunction = (answer : Answer) => void
- export type CandidateFunction = (candidate: Candidate) => void
- /**
- * Hub
- *
- * Represents a clients connection to the network. The hub
- * faciliates webrtc offer/answer/candidate exchange, and
- * also provides the ability for the client to request
- * their network address.
- */
- export class Hub {
- private _socket : Socket
- private _offer : OfferFunction = function() {}
- private _answer : AnswerFunction = function() {}
- private _candidate: CandidateFunction = function() {}
- private _address : string = ""
- private _flow : Flow = undefined
- /**
- * creates a new hub connection.
- * @param {string} endpoint the hub endpoint to connect to.
- * @returns {Hub}
- */
- constructor(private endpoint: string) {
- this._flow = new Flow()
- this._socket = new Socket(endpoint)
- this._socket.connect(this.connect.bind(this))
- this._socket.message(this.message.bind(this))
- this._socket.disconnect(this.disconnect.bind(this))
- }
- /**
- * returns this hubs address within the wider network.
- * @returns {Promise<string>}
- */
- public address (): Promise<string> {
- return this._flow.run(() => this._address)
- }
- /**
- * subscribes to incoming offers.
- * @param {OfferFunction} func the offer listening function.
- * @returns {void}
- */
- public onOffer (func: OfferFunction): void { this._offer = func }
- /**
- * subscribes to incoming answers.
- * @param {OfferFunction} func the answer listening function.
- * @returns {void}
- */
- public onAnswer (func: AnswerFunction): void { this._answer = func }
- /**
- * subscribes to incoming candidates.
- * @param {OfferFunction} func the candidate listening function.
- * @returns {void}
- */
- public onCandidate (func: CandidateFunction): void { this._candidate = func }
- /**
- * sends an offer to the network.
- * @param {Offer} offer the offer to send.
- * @returns {Promise<any>}
- */
- public sendOffer(offer: Offer): Promise<any> {
- return this._flow.run(() => this._socket.send(offer))
- }
- /**
- * sends an answer to the network.
- * @param {Answer} answer the answer to send.
- * @returns {Promise<any>}
- */
- public sendAnswer(answer: Answer): Promise<any> {
- return this._flow.run(() => this._socket.send(answer))
- }
- /**
- * sends a candidate to the network.
- * @param {Candidate} candidate the candidate to send.
- * @returns {Promise<any>}
- */
- public sendCandidate(candidate: Candidate): Promise<any> {
- return this._flow.run(() => this._socket.send(candidate))
- }
- /**
- * handles socket connection events.
- * @returns {void}
- */
- private connect () : void { }
- /**
- * handles socket disconnect events.
- * @returns {void}
- */
- private disconnect() : void { }
- /**
- * handles incoming network messages
- * @param {Message} message the incoming message.
- * @returns {void}
- */
- private message(message: Message): void {
- switch(message.type) {
- case "address": {
- this._address = message.address
- this._flow.resume()
- break;
- }
- case "offer": this._offer(message); break;
- case "answer": this._answer(message); break;
- case "candidate": this._candidate(message); break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement