服务端采用websocket实现
性能会不怎么好
后期将采用高性能框架进行开发
python服务端:
- # 打开终端输入:pip install websockets 下载模块即可
- import asyncio
- import websockets
- # 接收客户端消息并处理,这里只是简单把客户端发来的返回回去
- async def check_permit(websocket):
- while True:
- name = await websocket.recv()
- print("服务端收到的信息:" + name)
- greeting = "服务端处理完毕了"
- await websocket.send(greeting)
- # 服务器端主逻辑,可多加函数也可设置权限访问
- # websocket和path是该函数被回调时自动传过来的,不需要自己传
- async def main_socket(websocket, path):
- await check_permit(websocket)
- start_server = websockets.serve(main_socket, '0.0.0.0', 5000)
- asyncio.get_event_loop().run_until_complete(start_server)
- asyncio.get_event_loop().run_forever()
复制代码
懒人客户端:
- --本代码是直接复制 打开一个websocket连接 ,修改IP和端口即可
- local ws = nil
- function wLog(text)
- print(text)
- toast(text,0,0,20)
- end
- function onOpened(handle)
- ws = handle
- print("连接上服务器")
- end
- function reConnect()
- sleep(2000)
- wLog("断开连接,3秒后重连")
- setTimer(function()
- ws = nil
- startWebSocket("ws://192.168.200.108:5000",
- onOpened,onClosed,onError,onRecv)
- end,3000)
- end
- function onClosed(handle)
- reConnect()
- end
- function onError(handle)
- reConnect()
- end
- function onRecv(handle,message)
- local text = "消息:"..message
- wLog(text)
- end
- local handle = startWebSocket("ws://192.168.200.108:5000",
- 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)
- end
- end
复制代码
|