| 语法: |
startWebSocket(url,onOpened,onClosed,onError,onRecv) |
|
| 参数 |
类型 |
说明 |
| url |
字符串类型 |
ws地址,目前只支持ws协议wss暂不支持 |
| onOpened |
函数类型 |
当连接服务器成功后会回调这个函数 |
| onClosed |
函数类型 |
当连接服务器断开后会回调这个函数 |
| onError |
函数类型 |
当连接服务器失败后会回调这个函数 |
| onRecv |
函数类型 |
当有接收到数据时会回调这个函数 |
| return |
数字类型 |
表示这个websocket链接的句柄 |
| 使用事项 |
使用事项:以上所有回调函数是在单独的一个线程里面,请不要在回调函数中阻塞运行或者调用停止(exitScript)或者重启脚本(restartScript)函数,如果非要使用请在setTimer回调函数中执行, |
local ws = nilfunction wLog(text) print(text) toast(text,0,0,20)endfunction onOpened(handle) ws = handle print("连接上服务器")endfunction reConnect() ws = nil wLog("断开连接,3秒后重连") setTimer(function() startWebSocket("ws://192.168.2.105:5586",onOpened,onClosed,onError,onRecv)end,3000)endfunction onClosed(handle) reConnect()endfunction onError(handle) reConnect()endfunction onRecv(handle,message) local text = "消息:"..message wLog(text)endlocal handle = startWebSocket("ws://192.168.2.105:5586",onOpened,onClosed,onError,onRecv)if handle ~= nil then local tick = 1 while true do if ws ~= nil then sendWebSocket(ws,string.format("hello:%d",tick)) tick = tick + 1 end sleep(100) endend