WebSocket
HTML5 与 WebSocket
WebSocket协议在2011年被标准化,最初的目标是让浏览器与服务器建立稳定的双向连接.在此之前,浏览器曾只支持HTTPRequests,并不适合双向通信.
这个协议相当简单,基于消息,是一个非常强大的向浏览器推送通知的工具,已经被用来实现聊天、回合制游戏等.它仍使用TCP连接,这对可靠性有好处,但对延迟没有好处,所以不适合实时的应用,比如VoIP和快节奏的游戏(这些用例见 WebRTC).
由于它的简单性、广泛的兼容性以及比原始TCP连接更容易使用,WebSocket很快就开始在浏览器以外的地方应用,在本地应用程序中作为与网络服务器通信的一种手段.
Godot在本机和HTML5导出中都支持WebSocket.
在Godot中使用WebSocket
在Godot中,WebSocket通过三个主要类来实现 WebSocketClient, WebSocketServer, 和 WebSocketPeer.WebSocket的实现与高级多人游戏兼容.更多细节请参见 high-level multiplayer 一节.
最小客户端示例
本示例将向您展示如何创建与远程服务器的 WebSocket 连接,以及如何发送和接收数据.
extends Node
# The URL we will connect to
export var websocket_url = "ws://echo.websocket.org"
# Our WebSocketClient instance
var _client = WebSocketClient.new()
func _ready():
# Connect base signals to get notified of connection open, close, and errors.
_client.connect("connection_closed", self, "_closed")
_client.connect("connection_error", self, "_closed")
_client.connect("connection_established", self, "_connected")
# This signal is emitted when not using the Multiplayer API every time
# a full packet is received.
# Alternatively, you could check get_peer(1).get_available_packets() in a loop.
_client.connect("data_received", self, "_on_data")
# Initiate connection to the given URL.
var err = _client.connect_to_url(websocket_url)
if err != OK:
print("Unable to connect")
set_process(false)
func _closed(was_clean = false):
# was_clean will tell you if the disconnection was correctly notified
# by the remote peer before closing the socket.
print("Closed, clean: ", was_clean)
set_process(false)
func _connected(proto = ""):
# This is called on connection, "proto" will be the selected WebSocket
# sub-protocol (which is optional)
print("Connected with protocol: ", proto)
# You MUST always use get_peer(1).put_packet to send data to server,
# and not put_packet directly when not using the MultiplayerAPI.
_client.get_peer(1).put_packet("Test packet".to_utf8())
func _on_data():
# Print the received packet, you MUST always use get_peer(1).get_packet
# to receive data from server, and not get_packet directly when not
# using the MultiplayerAPI.
print("Got data from server: ", _client.get_peer(1).get_packet().get_string_from_utf8())
func _process(delta):
# Call this in _process or _physics_process. Data transfer, and signals
# emission will only happen when calling this function.
_client.poll()
这将打印:
Connected with protocol:
Got data from server: Test packet
最小服务器示例
本示例将向您展示如何创建一个监听远程连接的 WebSocket 服务器,以及如何发送和接收数据.
extends Node
# The port we will listen to
const PORT = 9080
# Our WebSocketServer instance
var _server = WebSocketServer.new()
func _ready():
# Connect base signals to get notified of new client connections,
# disconnections, and disconnect requests.
_server.connect("client_connected", self, "_connected")
_server.connect("client_disconnected", self, "_disconnected")
_server.connect("client_close_request", self, "_close_request")
# This signal is emitted when not using the Multiplayer API every time a
# full packet is received.
# Alternatively, you could check get_peer(PEER_ID).get_available_packets()
# in a loop for each connected peer.
_server.connect("data_received", self, "_on_data")
# Start listening on the given port.
var err = _server.listen(PORT)
if err != OK:
print("Unable to start server")
set_process(false)
func _connected(id, proto):
# This is called when a new peer connects, "id" will be the assigned peer id,
# "proto" will be the selected WebSocket sub-protocol (which is optional)
print("Client %d connected with protocol: %s" % [id, proto])
func _close_request(id, code, reason):
# This is called when a client notifies that it wishes to close the connection,
# providing a reason string and close code.
print("Client %d disconnecting with code: %d, reason: %s" % [id, code, reason])
func _disconnected(id, was_clean = false):
# This is called when a client disconnects, "id" will be the one of the
# disconnecting client, "was_clean" will tell you if the disconnection
# was correctly notified by the remote peer before closing the socket.
print("Client %d disconnected, clean: %s" % [id, str(was_clean)])
func _on_data(id):
# Print the received packet, you MUST always use get_peer(id).get_packet to receive data,
# and not get_packet directly when not using the MultiplayerAPI.
var pkt = _server.get_peer(id).get_packet()
print("Got data from client %d: %s ... echoing" % [id, pkt.get_string_from_utf8()])
_server.get_peer(id).put_packet(pkt)
func _process(delta):
# Call this in _process or _physics_process.
# Data transfer, and signals emission will only happen when calling this function.
_server.poll()
这将打印(当客户端连接时)与此类似的东西:
Client 1348090059 connected with protocol: selected-protocol
Got data from client 1348090059: Test packet ... echoing
高级聊天演示
在 godot demo projects 下 networking/websocket_chat 和 networking/websocket_multiplayer 有一个更高级的聊天演示demo,可以选择使用多人中级抽象和高级多人演示demo.