Skip to content

Godot、GDScript、Protobuf、Netty、ioGame、webSocket、java Netty Game Server.

License

Notifications You must be signed in to change notification settings

iohao/ioGameSdkGDScriptExampleGodot

Repository files navigation

中文

ioGameSdkGDScriptExampleGodot

The ioGame GDScript SDK provides a simple wrapper for interaction between the Netty, WebSocket, Protobuf, GDScript, and ioGame game servers.

The files such as action, broadcast, error codes, etc., in the ./gen/code directory are generated by ioGame. Code generation can significantly reduce the workload for client developers.

ioGame GDScript SDK + Code Generation = a variety of advantages

Advantages of SDK Code Generation

  1. Helps client-side developers reduce significant workload by eliminating the need to write a large amount of template code.
  2. Clear and semantically precise. The generated interaction code clearly defines parameter types and return types.
  3. Ensures parameter type safety and clarity in interface methods, effectively avoiding security risks and reducing basic errors during integration.
  4. Reduces communication costs between the server and client during integration; the code serves as documentation. The generated integration code includes documentation and usage examples, and the examples on the methods will guide you on how to use them, making it zero-learning-cost even for beginners.
  5. Helps client-side developers abstract away the interaction with the server, allowing them to focus more on the core business logic.
  6. Reduces the cognitive load during integration. The code is simple to use, similar to local method calls.
  7. Abandons the traditional protocol-based approach in favor of an interface-method-based integration approach.

Quick Start

Start the ioGame game server

see https://github.com/iohao/ioGameExamples/tree/main/SdkExample

Run SdkApplication.java

Start Godot

Godot Version: 4.4.1

Home

SDK Setup Instructions

The my_net_config.gd file handles the following:

  • Loading of error codes and broadcast listener related configurations.
  • IoGameSetting.listen_message_callback: Custom message listener.
  • socket_init(): Network implementation configuration and login.
  • IoGameSetting.start_net: Starts the ioGame SDK.
class_name MyNetConfig
extends RefCounted

static var _socket: MyNetChannel = MyNetChannel.new()
static var current_time_millis: int = 0

static func start_net():
	# biz code init
	GameCode.init()
	Listener.listener_ioGame()
	
	# --------- IoGameSetting ---------
	var setting := IoGame.IoGameSetting
	setting.enable_dev_mode = true
	setting.set_language(IoGame.IoGameLanguage.Us)
	# message callback. cn: 回调监听
	setting.listen_message_callback = MyListenMessageCallback.new()
	# set socket. cn: 设置网络连接
	setting.net_channel = _socket
	
	socket_init()
	setting.start_net()


static func poll():
	_socket.poll()

const Common = preload("res://gen/common.gd")

static func socket_init():
	_socket.on_open = func():
		var verify_message := Common.LoginVerifyMessage.new()
		verify_message.set_jwt("1")
		
		SdkAction.of_login_verify(verify_message, func(result: IoGame.ResponseResult):
				var value := result.get_value(Common.UserMessage) as Common.UserMessage
				print("user: ", value)
		)


static var _heartbeat_message_bytes := IoGame.Proto.ExternalMessage.new().to_bytes()
static var _heartbeat_counter: int = 1

static func send_idle() -> void:
	_heartbeat_counter += 1
	#print("-------- ..HeartbeatMessage {%s}" % [_heartbeat_counter])
	IoGame.IoGameSetting.net_channel.write_and_flush_byte(_heartbeat_message_bytes)


class MyListenMessageCallback extends IoGame.ListenMessageCallback:
	func on_idle_callback(message: Proto.ExternalMessage):
		var data_bytes := message.get_data()
		var long_value := IoGame.Proto.LongValue.new()
		long_value.from_bytes(data_bytes)
		# Synchronize the time of each heartbeat with that of the server.
		# cn: 每次心跳与服务器的时间同步
		MyNetConfig.current_time_millis = long_value.get_value()


class MyNetChannel extends IoGame.SimpleNetChannel:
	var _last_state: WebSocketPeer.State = WebSocketPeer.State.STATE_CLOSED
	var _socket: WebSocketPeer = WebSocketPeer.new()
	var _url: String = "ws://127.0.0.1:10100/websocket"

	var on_open: Callable = func():
		print("on_open")
	
	var on_connecting: Callable = func():
		print("on_connecting")

	var on_connect_error: Callable = func(error: int):
		print("on_connect_error:", error)

	var on_closing: Callable = func():
		print("on_closing")

	var on_closed: Callable = func():
		print("on_closed")

	func prepare() -> void:
		if _url == null or _url.is_empty():
			_url = IoGame.IoGameSetting.url

		var error: int = _socket.connect_to_url(_url)

		if error != 0:
			on_connect_error.call(error)
			return
		
		_last_state = _socket.get_ready_state()


	func write_and_flush_byte(bytes: PackedByteArray) -> void:
		_socket.send(bytes)


	func poll() -> void:
		if _socket.get_ready_state() != WebSocketPeer.State.STATE_CLOSED:
			_socket.poll()
		
		while _socket.get_available_packet_count() > 0:
			var packet := _socket.get_packet()
			var message := IoGame.Proto.ExternalMessage.new()
			message.from_bytes(packet)
			self.accept_message(message)
		
		var state: WebSocketPeer.State = _socket.get_ready_state()
		if _last_state == state:
			return
		
		_last_state = state

		match state:
			WebSocketPeer.State.STATE_OPEN:
				on_open.call()
			WebSocketPeer.State.STATE_CONNECTING:
				on_connecting.call()
			WebSocketPeer.State.STATE_CLOSING:
				on_closing.call()
			WebSocketPeer.State.STATE_CLOSED:
				on_closed.call()
			_:
				printerr("Socket Unknown Status.")

Precautions

Obtaining Network Data

see https://docs.godotengine.org/en/stable/tutorials/networking/websocket.html#using-websocket-in-godot

You need to call the MyNetConfig.poll() method within the _process(_delta) method to receive network data.

# see index.gd
extends Node

...

func _ready():
	MyNetConfig.start_net()

func _process(_delta):
    # cn: 接收网络数据
	# Call this in _process or _physics_process.
	# Data transfer, and signals emission will only happen when calling this function.
	MyNetConfig.poll()

Interface Source Code Directory

The files such as action, broadcast, error codes, etc., in the ./gen/code directory are generated by ioGame. Code generation can significantly reduce the workload for client developers.

How to generate .proto code

see https://github.com/oniksan/godobuf

or SDK GDScript proto

Finally

Remember, you don't need to write any interaction files (actions, broadcasts, error codes). These are generated by the ioGame server. You only need to focus on the actual business logic.

How to Install the ioGame GDScript SDK in a New Project

The current demo already has the ioGame GDScript SDK installed. The following describes how to install the SDK in a new project.

Search for "ooGame" through AssetLib and download it.

Click the "Install" button to install the SDK into the addons plugin directory.

After installation, the addons directory will contain the SDK-related files.

About

Godot、GDScript、Protobuf、Netty、ioGame、webSocket、java Netty Game Server.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published