/**
 * Copyright 2020 The Nakama Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ /**
 * An interface used by Nakama's web socket to determine the payload protocol.
 */ export interface WebSocketAdapter { /**
     * Dispatched when the web socket closes.
     */ onClose : SocketCloseHandler | null ; /**
     * Dispatched when the web socket receives an error.
     */ onError : SocketErrorHandler | null ; /**
     * Dispatched when the web socket receives a normal message.
     */ onMessage : SocketMessageHandler | null ; /**
     * Dispatched when the web socket opens.
     */ onOpen : SocketOpenHandler | null ; readonly isConnected : boolean ; close ( ) : void ; connect ( scheme , host , port , createStatus , token ) : void ; send ( message ) : void ; } /**
 * SocketCloseHandler defines a lambda that handles WebSocket close events.
 */ export interface SocketCloseHandler { ( this , evt ) : void ; } /**
 * SocketErrorHandler defines a lambda that handles responses from the server via WebSocket
 * that indicate an error.
 */ export interface SocketErrorHandler { ( this , evt ) : void ; } /**
 * SocketMessageHandler defines a lambda that handles valid WebSocket messages.
 */ export interface SocketMessageHandler { ( message ) : void ; } /**
 * SocketOpenHandler defines a lambda that handles WebSocket open events.
 */ export interface SocketOpenHandler { ( this , evt ) : void ; } /**
 * A text-based socket adapter that accepts and transmits payloads over UTF-8.
 */ export class WebSocketAdapterText implements WebSocketAdapter { private _isConnected : boolean = false ; private _socket ? : WebSocket ; get onClose ( ) : SocketCloseHandler | null { return this . _socket ! . onclose ; } set onClose ( value ) { this . _socket ! . onclose = value ; } get onError ( ) : SocketErrorHandler | null { return this . _socket ! . onerror ; } set onError ( value ) { this . _socket ! . onerror = value ; } get onMessage ( ) : SocketMessageHandler | null { return this . _socket ! . onmessage ; } set onMessage ( value ) { if ( value ) { this . _socket ! . onmessage = ( evt ) => { const message = JSON . parse ( evt . data ) ; value ! ( message ) ; } ; } else { value = null ; } } get onOpen ( ) : SocketOpenHandler | null { return this . _socket ! . onopen ; } set onOpen ( value ) { this . _socket ! . onopen = value ; } get isConnected ( ) : boolean { return this . _isConnected ; } connect ( scheme , host , port , createStatus , token ) { const url = `${ scheme }${ host }:${ port }/ws?lang=en&status=${ encodeURIComponent ( createStatus . toString ( ) ) }&token=${ encodeURIComponent ( token ) }` ; this . _socket = new WebSocket ( url ) ; this . _isConnected = true ; } close ( ) { this . _isConnected = false ; this . _socket ! . close ( ) ; this . _socket = undefined ; } send ( msg ) { if ( msg . match_data_send ) { msg . match_data_send . op_code = msg . match_data_send . op_code . toString ( ) ; } else if ( msg . party_data_send ) { msg . party_data_send . op_code = msg . party_data_send . op_code . toString ( ) ; } this . _socket ! . send ( JSON . stringify ( msg ) ) ; } }