如何使用Lua脚本语言打开Web套接字?

时间:2013-06-17 09:41:12

标签: lua websocket

作为初学者,我想在基于Linux的服务器上使用Lua打开一个Web套接字。此服务器应允许Android客户端连接到它。你能给我一些用Lua打开网络套接字的示例代码吗?

1 个答案:

答案 0 :(得分:7)

两周前你已经回答了同样的问题:LUA Script - web socket communication。你看过lua-websockets吗?你有什么尝试?什么不起作用?

Examples from the websockets module我之前提到过:

-- 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')

你试过吗?陷入困境?