【发布时间】:2017-03-22 18:55:02
【问题描述】:
我是 lua 编程新手,并尝试在 openwrt 中作为客户端实现 Lua-websocket。 here is the library. 试图使用客户端 copas 库,但问题是脚本在执行一次后停止监听服务器(即连接到服务器、接收消息、发送消息)。我希望脚本始终在没有任何超时或脚本停止的情况下监听服务器。 下面是脚本
local copas = require'copas'
local websocket = require'websocket'
local json = require('json')
local client = require 'websocket.client'.new()
local ok,err = client:connect('ws://192.168.1.250:8080')
if not ok then
print('could not connect',err)
end
local ap_mac = { command = 'subscribe', channel = 'test' }
local ok = client:send(json.encode(ap_mac))
if ok then
print('msg sent')
else
print('connection closed')
end
local message,opcode = client:receive()
if message then
print('msg',message,opcode)
else
print('connection closed')
end
local replymessage = { command = 'message', message = 'TEST' }
local ok = client:send(json.encode(replymessage))
if ok then
print('msg sent')
else
print('connection closed')
end
copas.loop()
这里 copas.loop() 不起作用。
在 openWrt 上我安装了 lua 5.1
【问题讨论】: