websocket API
This commit is contained in:
@@ -1569,6 +1569,219 @@ var HTML_CONTENTa = """<head>
|
||||
""".to_utf8_buffer()
|
||||
|
||||
var HTML_CONTENT = """<head>
|
||||
<title>WebSocket API Demo</title>
|
||||
<icon src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/cf/Lua-Logo.svg/256px-Lua-Logo.svg.png">
|
||||
<meta name="theme-color" content="#059669">
|
||||
<meta name="description" content="Demonstrating WebSocket real-time communication API">
|
||||
|
||||
<style>
|
||||
body { bg-[#f0fdf4] p-6 }
|
||||
h1 { text-[#059669] text-3xl font-bold text-center }
|
||||
h2 { text-[#047857] text-xl font-semibold }
|
||||
.container { bg-[#ffffff] p-6 rounded-lg shadow-lg max-w-4xl mx-auto }
|
||||
.button-group { flex gap-3 justify-center items-center flex-wrap }
|
||||
.ws-button { px-4 py-2 rounded-lg font-medium cursor-pointer transition-colors bg-[#10b981] text-white hover:bg-[#059669] }
|
||||
.send-button { px-4 py-2 rounded-lg font-medium cursor-pointer transition-colors bg-[#3b82f6] text-white hover:bg-[#2563eb] }
|
||||
.disconnect-button { px-4 py-2 rounded-lg font-medium cursor-pointer transition-colors bg-[#ef4444] text-white hover:bg-[#dc2626] }
|
||||
.clear-button { px-4 py-2 rounded-lg font-medium cursor-pointer transition-colors bg-[#6b7280] text-white hover:bg-[#4b5563] }
|
||||
.log-area { bg-[#f1f5f9] p-4 rounded-lg min-h-32 font-mono text-sm max-h-96 overflow-auto }
|
||||
.info-box { bg-[#dbeafe] border border-[#3b82f6] p-4 rounded-lg }
|
||||
.input-area { bg-[#f9fafb] p-4 rounded-lg border }
|
||||
.status-connected { text-[#059669] font-bold }
|
||||
.status-disconnected { text-[#ef4444] font-bold }
|
||||
.status-connecting { text-[#f59e0b] font-bold }
|
||||
</style>
|
||||
|
||||
<script>
|
||||
-- Get UI elements
|
||||
local logArea = gurt.select('#log-area')
|
||||
local statusDisplay = gurt.select('#status-display')
|
||||
local connectBtn = gurt.select('#connect-btn')
|
||||
local disconnectBtn = gurt.select('#disconnect-btn')
|
||||
local sendBtn = gurt.select('#send-btn')
|
||||
local clearLogBtn = gurt.select('#clear-log-btn')
|
||||
local urlInput = gurt.select('#url-input')
|
||||
local messageInput = gurt.select('#message-input')
|
||||
|
||||
gurt.log('WebSocket API demo script started.')
|
||||
|
||||
local logMessages = {}
|
||||
local socket = nil
|
||||
local connected = false
|
||||
|
||||
-- Function to add message to log
|
||||
local function addLog(message)
|
||||
table.insert(logMessages, Time.format(Time.now(), '%H:%M:%S') .. ' - ' .. message)
|
||||
if #logMessages > 50 then
|
||||
table.remove(logMessages, 1)
|
||||
end
|
||||
logArea.text = table.concat(logMessages, '\\n')
|
||||
end
|
||||
|
||||
-- Function to update status display
|
||||
local function updateStatus(status, color)
|
||||
statusDisplay.text = 'Status: ' .. status
|
||||
statusDisplay.style = 'status-' .. color
|
||||
end
|
||||
|
||||
-- Initialize with default values
|
||||
urlInput.text = 'wss://echo.websocket.org'
|
||||
messageInput.text = 'Hello from Gurted!'
|
||||
|
||||
-- Connect to WebSocket
|
||||
connectBtn:on('click', function()
|
||||
local url = urlInput.text
|
||||
addLog('Attempting to connect to: ' .. url)
|
||||
updateStatus('Connecting...', 'connecting')
|
||||
|
||||
socket = WebSocket:new(url)
|
||||
|
||||
socket:on('open', function()
|
||||
addLog('✅ Connected to WebSocket server!')
|
||||
updateStatus('Connected', 'connected')
|
||||
connected = true
|
||||
socket:send('Hello from the Gurted web!')
|
||||
end)
|
||||
|
||||
socket:on('message', function(event)
|
||||
addLog('📨 Received: ' .. event.data)
|
||||
end)
|
||||
|
||||
socket:on('close', function()
|
||||
addLog('❌ Connection closed')
|
||||
updateStatus('Disconnected', 'disconnected')
|
||||
connected = false
|
||||
end)
|
||||
|
||||
socket:on('error', function(event)
|
||||
addLog('🚨 Error: ' .. (event.message or 'Unknown error'))
|
||||
updateStatus('Error', 'disconnected')
|
||||
connected = false
|
||||
end)
|
||||
end)
|
||||
|
||||
-- Disconnect from WebSocket
|
||||
disconnectBtn:on('click', function()
|
||||
if socket and connected then
|
||||
addLog('Disconnecting from WebSocket...')
|
||||
socket:close()
|
||||
else
|
||||
addLog('No active connection to disconnect')
|
||||
end
|
||||
end)
|
||||
|
||||
-- Send message
|
||||
sendBtn:on('click', function()
|
||||
if socket and connected then
|
||||
local message = messageInput.text
|
||||
if message and message ~= '' then
|
||||
socket:send(message)
|
||||
addLog('📤 Sent: ' .. message)
|
||||
else
|
||||
addLog('❌ Cannot send empty message')
|
||||
end
|
||||
else
|
||||
addLog('❌ Not connected to WebSocket server')
|
||||
end
|
||||
end)
|
||||
|
||||
-- Clear log button
|
||||
clearLogBtn:on('click', function()
|
||||
logMessages = {}
|
||||
logArea.text = 'Log cleared.'
|
||||
addLog('WebSocket API demo ready')
|
||||
end)
|
||||
|
||||
-- Allow Enter key to send messages
|
||||
messageInput:on('keypress', function(e)
|
||||
if e.key == 'Enter' and socket and connected then
|
||||
local message = messageInput.text
|
||||
if message and message ~= '' then
|
||||
socket:send(message)
|
||||
addLog('📤 Sent: ' .. message)
|
||||
messageInput.text = ''
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
-- Initialize
|
||||
updateStatus('Disconnected', 'disconnected')
|
||||
addLog('WebSocket API demo ready')
|
||||
addLog('Enter a WebSocket URL and click Connect to start!')
|
||||
</script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>🔌 WebSocket API Demo</h1>
|
||||
|
||||
<div style="container mt-6">
|
||||
<div style="info-box mb-6">
|
||||
<h3 style="text-[#1e40af] font-semibold mb-2">WebSocket API Usage:</h3>
|
||||
<p><code>local socket = WebSocket:new(url)</code></p>
|
||||
<p><code>socket:on('open', function() ... end)</code></p>
|
||||
<p><code>socket:on('message', function(event) print(event.data) end)</code></p>
|
||||
<p><code>socket:send('Hello Server!')</code></p>
|
||||
<p><code>socket:close()</code></p>
|
||||
</div>
|
||||
|
||||
<h2>Connection</h2>
|
||||
<div style="input-area mb-6">
|
||||
<div style="flex flex-col gap-3">
|
||||
<div>
|
||||
<label style="block text-sm font-medium mb-1">WebSocket URL:</label>
|
||||
<input id="url-input" type="text" style="w-full p-2 border border-gray-300 rounded-md" placeholder="ws://echo.websocket.org" />
|
||||
</div>
|
||||
<div id="status-display" style="status-disconnected">Status: Disconnected</div>
|
||||
<div style="flex gap-2">
|
||||
<button id="connect-btn" style="ws-button">🔗 Connect</button>
|
||||
<button id="disconnect-btn" style="disconnect-button">❌ Disconnect</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2>Send Messages</h2>
|
||||
<div style="input-area mb-6">
|
||||
<div style="flex flex-col gap-3">
|
||||
<div>
|
||||
<label style="block text-sm font-medium mb-1">Message:</label>
|
||||
<input id="message-input" type="text" style="w-full p-2 border border-gray-300 rounded-md" placeholder="Type your message..." />
|
||||
</div>
|
||||
<div style="flex gap-2">
|
||||
<button id="send-btn" style="send-button">📤 Send Message</button>
|
||||
<span style="text-sm text-gray-600">Or press Enter to send</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2>Event Log</h2>
|
||||
<div style="button-group mb-3">
|
||||
<button id="clear-log-btn" style="clear-button">🧹 Clear Log</button>
|
||||
</div>
|
||||
<div style="log-area mb-6">
|
||||
<pre id="log-area">Initializing...</pre>
|
||||
</div>
|
||||
|
||||
<div style="bg-[#f0f9ff] p-4 rounded-lg">
|
||||
<h3 style="text-[#0369a1] font-semibold mb-2">WebSocket Events:</h3>
|
||||
<ul style="text-[#075985] space-y-1 text-sm">
|
||||
<li><strong>open:</strong> Fired when connection is established</li>
|
||||
<li><strong>message:</strong> Fired when a message is received (event.data contains the message)</li>
|
||||
<li><strong>close:</strong> Fired when connection is closed</li>
|
||||
<li><strong>error:</strong> Fired when an error occurs (event.message contains error info)</li>
|
||||
</ul>
|
||||
<h3 style="text-[#0369a1] font-semibold mt-4 mb-2">WebSocket Methods:</h3>
|
||||
<ul style="text-[#075985] space-y-1 text-sm">
|
||||
<li><strong>WebSocket:new(url):</strong> Creates and connects to WebSocket server</li>
|
||||
<li><strong>socket:send(message):</strong> Sends a text message to the server</li>
|
||||
<li><strong>socket:close():</strong> Closes the connection</li>
|
||||
<li><strong>socket:on(event, callback):</strong> Registers event handlers</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
""".to_utf8_buffer()
|
||||
|
||||
var HTML_CONTENTy = """<head>
|
||||
<title>Network & JSON API Demo</title>
|
||||
<icon src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/cf/Lua-Logo.svg/256px-Lua-Logo.svg.png">
|
||||
<meta name="theme-color" content="#0891b2">
|
||||
|
||||
@@ -122,10 +122,7 @@ func _lua_thread_worker():
|
||||
lua_vm.lua_setfield(-2, "sleep")
|
||||
lua_vm.lua_setglobal("Time")
|
||||
|
||||
# Setup GURT API with thread-safe versions
|
||||
_setup_threaded_gurt_api()
|
||||
|
||||
# Setup additional API functions that are needed in callbacks
|
||||
_setup_additional_lua_apis()
|
||||
|
||||
while not should_exit:
|
||||
@@ -320,17 +317,11 @@ func _setup_additional_lua_apis():
|
||||
lua_vm.lua_setfield(-2, "tostring")
|
||||
lua_vm.lua_pop(1) # Pop table from stack
|
||||
|
||||
# Setup Signal API for threaded execution
|
||||
LuaSignalUtils.setup_signal_api(lua_vm)
|
||||
|
||||
# Setup Clipboard API for threaded execution
|
||||
LuaClipboardUtils.setup_clipboard_api(lua_vm)
|
||||
|
||||
# Setup Network API for threaded execution
|
||||
LuaNetworkUtils.setup_network_api(lua_vm)
|
||||
|
||||
# Setup JSON API for threaded execution
|
||||
LuaJSONUtils.setup_json_api(lua_vm)
|
||||
LuaWebSocketUtils.setup_websocket_api(lua_vm)
|
||||
|
||||
func _threaded_table_tostring_handler(vm: LuauVM) -> int:
|
||||
vm.luaL_checktype(1, vm.LUA_TTABLE)
|
||||
|
||||
260
flumi/Scripts/Utils/Lua/WebSocket.gd
Normal file
260
flumi/Scripts/Utils/Lua/WebSocket.gd
Normal file
@@ -0,0 +1,260 @@
|
||||
class_name LuaWebSocketUtils
|
||||
extends RefCounted
|
||||
|
||||
static var websocket_instances: Dictionary = {}
|
||||
static var instance_counter: int = 0
|
||||
|
||||
class WebSocketWrapper:
|
||||
extends RefCounted
|
||||
|
||||
var instance_id: String
|
||||
var vm: LuauVM
|
||||
var url: String
|
||||
var websocket: WebSocketPeer
|
||||
var is_connected: bool = false
|
||||
var event_handlers: Dictionary = {}
|
||||
var timer: Timer
|
||||
var last_state: int = -1
|
||||
|
||||
func _init():
|
||||
websocket = WebSocketPeer.new()
|
||||
|
||||
func connect_to_url():
|
||||
if is_connected:
|
||||
return
|
||||
|
||||
var error = websocket.connect_to_url(url)
|
||||
|
||||
if error == OK:
|
||||
# Start polling timer
|
||||
if timer:
|
||||
timer.queue_free()
|
||||
|
||||
timer = Timer.new()
|
||||
timer.wait_time = 0.016 # ~60 FPS polling
|
||||
timer.timeout.connect(_poll_websocket)
|
||||
|
||||
# Add to scene tree using call_deferred for thread safety
|
||||
var main_loop = Engine.get_main_loop()
|
||||
|
||||
if main_loop and main_loop.current_scene:
|
||||
main_loop.current_scene.call_deferred("add_child", timer)
|
||||
timer.call_deferred("start")
|
||||
else:
|
||||
trigger_event("error", {"message": "No scene available for WebSocket timer"})
|
||||
else:
|
||||
trigger_event("error", {"message": "Failed to connect to " + url + " (error: " + str(error) + ")"})
|
||||
|
||||
func _poll_websocket():
|
||||
if not websocket:
|
||||
return
|
||||
|
||||
websocket.poll()
|
||||
var state = websocket.get_ready_state()
|
||||
|
||||
match state:
|
||||
WebSocketPeer.STATE_OPEN:
|
||||
if not is_connected:
|
||||
is_connected = true
|
||||
trigger_event("open", {})
|
||||
|
||||
# Check for messages
|
||||
while websocket.get_available_packet_count() > 0:
|
||||
var packet = websocket.get_packet()
|
||||
var message = packet.get_string_from_utf8()
|
||||
trigger_event("message", {"data": message})
|
||||
|
||||
WebSocketPeer.STATE_CLOSED:
|
||||
if is_connected:
|
||||
is_connected = false
|
||||
trigger_event("close", {})
|
||||
|
||||
# Clean up timer
|
||||
if timer:
|
||||
timer.queue_free()
|
||||
timer = null
|
||||
|
||||
WebSocketPeer.STATE_CONNECTING:
|
||||
# Still connecting, keep polling
|
||||
pass
|
||||
|
||||
WebSocketPeer.STATE_CLOSING:
|
||||
# Connection is closing
|
||||
if is_connected:
|
||||
is_connected = false
|
||||
|
||||
_:
|
||||
# Unknown state or connection failed
|
||||
if is_connected:
|
||||
is_connected = false
|
||||
trigger_event("close", {})
|
||||
elif not is_connected:
|
||||
# This might be a connection failure
|
||||
trigger_event("error", {"message": "Connection failed or was rejected by server"})
|
||||
|
||||
func send_message(message: String):
|
||||
if is_connected and websocket:
|
||||
websocket.send_text(message)
|
||||
|
||||
func close_connection():
|
||||
if websocket:
|
||||
websocket.close()
|
||||
is_connected = false
|
||||
|
||||
if timer:
|
||||
timer.queue_free()
|
||||
timer = null
|
||||
|
||||
func trigger_event(event_name: String, data: Dictionary):
|
||||
if not vm or not event_handlers.has(event_name):
|
||||
return
|
||||
|
||||
var func_ref = event_handlers[event_name]
|
||||
|
||||
# Get the function from the reference
|
||||
vm.lua_getref(func_ref)
|
||||
|
||||
if vm.lua_isfunction(-1):
|
||||
# Create event data table
|
||||
vm.lua_newtable()
|
||||
for key in data:
|
||||
vm.lua_pushvariant(data[key])
|
||||
vm.lua_setfield(-2, key)
|
||||
|
||||
# Call the function
|
||||
var result = vm.lua_pcall(1, 0, 0)
|
||||
if result != vm.LUA_OK:
|
||||
vm.lua_pop(1)
|
||||
else:
|
||||
vm.lua_pop(1) # Pop the non-function value
|
||||
|
||||
static func setup_websocket_api(vm: LuauVM):
|
||||
vm.lua_newtable()
|
||||
vm.lua_pushcallable(_websocket_new, "WebSocket.new")
|
||||
vm.lua_setfield(-2, "new")
|
||||
vm.lua_setglobal("WebSocket")
|
||||
|
||||
static func _websocket_new(vm: LuauVM) -> int:
|
||||
# handle both WebSocket:new(url) and WebSocket.new(url) syntax
|
||||
var url: String
|
||||
if vm.lua_gettop() == 2 and vm.lua_istable(1):
|
||||
url = vm.luaL_checkstring(2)
|
||||
else:
|
||||
url = vm.luaL_checkstring(1)
|
||||
|
||||
# Generate unique instance ID
|
||||
instance_counter += 1
|
||||
var instance_id = "ws_" + str(instance_counter)
|
||||
|
||||
# Create WebSocket wrapper object
|
||||
var wrapper = WebSocketWrapper.new()
|
||||
wrapper.instance_id = instance_id
|
||||
wrapper.vm = vm
|
||||
wrapper.url = url
|
||||
|
||||
# Store in global instances to keep it alive
|
||||
websocket_instances[instance_id] = wrapper
|
||||
|
||||
# Create Lua table for the WebSocket
|
||||
vm.lua_newtable()
|
||||
|
||||
# Store instance ID
|
||||
vm.lua_pushstring(instance_id)
|
||||
vm.lua_setfield(-2, "_instance_id")
|
||||
|
||||
# Store URL
|
||||
vm.lua_pushstring(url)
|
||||
vm.lua_setfield(-2, "_url")
|
||||
|
||||
# Store connection state
|
||||
vm.lua_pushboolean(false)
|
||||
vm.lua_setfield(-2, "_connected")
|
||||
|
||||
# Initialize event handlers table
|
||||
vm.lua_newtable()
|
||||
vm.lua_setfield(-2, "_event_handlers")
|
||||
|
||||
# Add methods
|
||||
vm.lua_pushcallable(_websocket_on, "websocket.on")
|
||||
vm.lua_setfield(-2, "on")
|
||||
|
||||
vm.lua_pushcallable(_websocket_send, "websocket.send")
|
||||
vm.lua_setfield(-2, "send")
|
||||
|
||||
vm.lua_pushcallable(_websocket_close, "websocket.close")
|
||||
vm.lua_setfield(-2, "close")
|
||||
|
||||
vm.lua_pushcallable(_websocket_connect, "websocket.connect")
|
||||
vm.lua_setfield(-2, "connect")
|
||||
|
||||
# Auto-connect
|
||||
wrapper.connect_to_url()
|
||||
|
||||
return 1
|
||||
|
||||
static func _websocket_on(vm: LuauVM) -> int:
|
||||
vm.luaL_checktype(1, vm.LUA_TTABLE)
|
||||
var event_name: String = vm.luaL_checkstring(2)
|
||||
vm.luaL_checktype(3, vm.LUA_TFUNCTION)
|
||||
|
||||
# Get instance ID
|
||||
vm.lua_getfield(1, "_instance_id")
|
||||
var instance_id: String = vm.lua_tostring(-1)
|
||||
vm.lua_pop(1)
|
||||
|
||||
# Get wrapper instance
|
||||
var wrapper: WebSocketWrapper = websocket_instances.get(instance_id)
|
||||
if wrapper:
|
||||
# Store the function reference in wrapper
|
||||
var func_ref = vm.lua_ref(3)
|
||||
wrapper.event_handlers[event_name] = func_ref
|
||||
|
||||
return 0
|
||||
|
||||
static func _websocket_send(vm: LuauVM) -> int:
|
||||
vm.luaL_checktype(1, vm.LUA_TTABLE)
|
||||
var message: String = vm.luaL_checkstring(2)
|
||||
|
||||
# Get instance ID
|
||||
vm.lua_getfield(1, "_instance_id")
|
||||
var instance_id: String = vm.lua_tostring(-1)
|
||||
vm.lua_pop(1)
|
||||
|
||||
# Get wrapper instance
|
||||
var wrapper: WebSocketWrapper = websocket_instances.get(instance_id)
|
||||
if wrapper and wrapper.is_connected:
|
||||
wrapper.send_message(message)
|
||||
else:
|
||||
vm.luaL_error("WebSocket is not connected")
|
||||
|
||||
return 0
|
||||
|
||||
static func _websocket_close(vm: LuauVM) -> int:
|
||||
vm.luaL_checktype(1, vm.LUA_TTABLE)
|
||||
|
||||
# Get instance ID
|
||||
vm.lua_getfield(1, "_instance_id")
|
||||
var instance_id: String = vm.lua_tostring(-1)
|
||||
vm.lua_pop(1)
|
||||
|
||||
# Get wrapper instance
|
||||
var wrapper: WebSocketWrapper = websocket_instances.get(instance_id)
|
||||
if wrapper:
|
||||
wrapper.close_connection()
|
||||
|
||||
return 0
|
||||
|
||||
static func _websocket_connect(vm: LuauVM) -> int:
|
||||
vm.luaL_checktype(1, vm.LUA_TTABLE)
|
||||
|
||||
# Get instance ID
|
||||
vm.lua_getfield(1, "_instance_id")
|
||||
var instance_id: String = vm.lua_tostring(-1)
|
||||
vm.lua_pop(1)
|
||||
|
||||
# Get wrapper instance
|
||||
var wrapper: WebSocketWrapper = websocket_instances.get(instance_id)
|
||||
if wrapper:
|
||||
wrapper.connect_to_url()
|
||||
|
||||
return 0
|
||||
1
flumi/Scripts/Utils/Lua/WebSocket.gd.uid
Normal file
1
flumi/Scripts/Utils/Lua/WebSocket.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://boar4c3uf4x1e
|
||||
Reference in New Issue
Block a user