如何使用Lua脚本语言打开Web Socket连接?

作为一个初学者,我想在基于 Linux 的服务器上使用 Lua 打开一个 Web Socket。该服务器应该允许 Android 客户端连接。你能否给我一些打开 Web Socket 的 Lua 示例代码呢?

点赞
用户1442917
用户1442917

你两周前已经问过相同的问题,这个问题已经被回答了:LUA Script - web socket communication。你看过 lua-websockets 吗?你尝试过什么?有什么问题没有解决?

我先前提到的 websockets 模块例子:

-- create client:

local websocket = require'websocket'
local client = websocket.client.copas({timeout=2})

-- connect to the server:

local ok,err = client:connect('ws://localhost:12345','echo')
if not ok then
   print('could not connect',err)
end

-- send data:

local ok = client:send('hello')
if ok then
   print('msg sent')
else
   print('connection closed')
end

-- receive data:

local message,opcode = client:receive()
if message then
   print('msg',message,opcode)
else
   print('connection closed')
end

-- close connection:

local close_was_clean,close_code,close_reason = client:close(4001,'lost interest')

你试过了吗?是否遇到问题?

2013-06-17 14:58:13