vanilladocs
vanillapunch.com
API / web/websocketclient

web/websocketclient

웹소켓 통신 처리기.

WebSocket communication handler.

WebSocketClientextends Object

짝Counterpart: vanilla-engine WebSocketClient

웹소켓 통신 처리기.

  • 받은 JSON 은 수신 콜백이 있으면 그것을 부르고, 없으면 모아 두었다가 takeReceivedMessages 로 꺼낸다. (매 프레임 꺼내 쓰는 게임용)

WebSocket communication handler.

  • A received JSON is passed to the receive callback if there is one, otherwise it is collected and taken out with takeReceivedMessages. (for games that take them out every frame)
예제Example받은 JSON 을 콜백으로 받거나 모아서 꺼내기Receive received JSON with a callback or collect and take it out
import { WebSocketClient } from "./build/vanilla.js";

const client = new WebSocketClient();
client.receiveMessage({ type: "hello" });
console.log(client.isConnected(), client.takeReceivedMessages().length);
client.setReceiveEvent((message) => {
	console.log(message.type);
});
client.receiveMessage({ type: "ping" });
console.log(client.getReceiveEvent() !== null);
생성자constructorconstructor()#0.0.11 부터since 0.0.11소스source

생성.

Constructor.

짝Counterpart: vanilla-engine WebSocketClient.new

메서드methodconnect(url)#0.0.11 부터since 0.0.11소스source

접속.

Connects.

urlstring
접속할 웹소켓 주소. (ws:// 나 wss://, 이미 접속 중이면 무시한다)The WebSocket address to connect to. (ws:// or wss://, ignored if already connected)

짝Counterpart: vanilla-engine WebSocketClient.connect

메서드methodreceiveMessage(obj)#0.4.0 부터since 0.4.0소스source

받은 JSON 넘기기. (수신 콜백이 있으면 콜백, 없으면 모아 두었다가 takeReceivedMessages 로 꺼낸다)

Hands over received JSON. (to the callback if there is one, otherwise collected and taken out with takeReceivedMessages)

objobject
받은 JSON 객체. (수신 콜백이 있으면 콜백으로, 없으면 모아 둔다)The received JSON object. (to the receive callback if there is one, otherwise collected)

짝Counterpart: vanilla-engine WebSocketClient.receive_message

메서드methodtakeReceivedMessages()#0.4.0 부터since 0.4.0소스source

모아 둔 받은 메시지 꺼내기. (수신 콜백이 없을 때 모인 것, 받은 차례, 꺼내면 비운다)

Takes out the collected received messages. (those collected while there was no receive callback, in the order received, emptied once taken)

반환ReturnsArray<object> 모아 둔 받은 메시지. (받은 차례, 꺼내면 비운다)The collected received messages. (in the order received, emptied once taken)

짝Counterpart: vanilla-engine WebSocketClient.take_received_messages

메서드methodtick()#0.4.0 부터since 0.4.0소스source

갱신. (브라우저가 사건을 그때그때 넘겨 할 일이 없다, 매 프레임 사건을 꺼내야 하는 플랫폼과 같은 코드로 쓰려고 둔다)

Update. (the browser hands events over as they happen so there is nothing to do, it exists so the same code works on platforms that must take events every frame)

짝Counterpart: vanilla-engine WebSocketClient.tick

메서드methoddisconnect()#0.0.11 부터since 0.0.11소스source

접속 해제.

Disconnects.

짝Counterpart: vanilla-engine WebSocketClient.disconnect

메서드methodsend(obj)#0.0.11 부터since 0.0.11소스source

JSON 송신.

Sends JSON.

objobject
보낼 JSON 객체. (접속 중이 아니면 보내지 않는다)The JSON object to send. (not sent if not connected)

짝Counterpart: vanilla-engine WebSocketClient.send

메서드methodisConnected()#0.0.11 부터since 0.0.11소스source

접속 여부 반환.

Returns whether it is connected.

반환Returnsboolean 접속 중이면 참.True if connected.

짝Counterpart: vanilla-engine WebSocketClient.is_connected

메서드methodgetWebSocket()#0.0.11 부터since 0.0.11소스source

웹소켓 반환.

Returns the WebSocket.

반환ReturnsWebSocket | null 안쪽 웹소켓. (접속하지 않았으면 null)The inner WebSocket. (null if not connected)

짝Counterpart: vanilla-engine WebSocketClient.get_web_socket

메서드methodsetReceiveEvent(callback)#0.0.11 부터since 0.0.11소스source

수신 이벤트 설정.

Sets the receive event.

callbackFunction | null
받은 JSON 객체를 넘겨 받을 함수. (null 이면 모아 두는 쪽으로 돌아간다)The function that receives the received JSON object. (null goes back to collecting)

짝Counterpart: vanilla-engine WebSocketClient.set_receive_event

메서드methodgetReceiveEvent()#0.0.11 부터since 0.0.11소스source

수신 이벤트 반환.

Returns the receive event.

반환ReturnsFunction | null 수신 콜백. (없으면 null)The receive callback. (null if none)

짝Counterpart: vanilla-engine WebSocketClient.get_receive_event