基于websocket实现多端即时通讯小示例

[复制链接]
查看1948 | 回复3 | 2022-5-12 22:35:55 | 显示全部楼层 |阅读模式
基于websocket实现多端即时通讯小示例,可以实现多个客户端的即时通讯.
python 源码需要先安装一下:
pip install SimpleWebSocketServer 第三方库

synchronizer.py 文件源码:
  1. import json
  2. from SimpleWebSocketServer import WebSocket, SimpleWebSocketServer
  3. wss = []
  4. class SimpleEcho(WebSocket):
  5.     # 接收设备发送来的数据
  6.     def handleMessage(self):
  7.         if self.data is None:
  8.             self.data = ''
  9.         print(self.address,'发送的数据:',self.data)
  10.         # 接收数据 广播给所以 连接设备
  11.         for i in wss:
  12.             i.sendMessage(json.dumps({'code':200,'msg':self.data},ensure_ascii=False))

  13.     def handleConnected(self):
  14.         # 客户端连接打印 ip:端口
  15.         print(self.address, '设备连接成功')
  16.         if not self in wss:
  17.             wss.append(self)

  18.     def handleClose(self):
  19.         # 客户端关闭后打印
  20.         print(self.address, '设备已断开')

  21.     @staticmethod
  22.     def run():
  23.         print('websocket 服务器启动中...')
  24.         server = SimpleWebSocketServer('0.0.0.0', 8000, SimpleEcho)
  25.         server.serveforever()
  26. if __name__ == '__main__':
  27.     SimpleEcho.run()
复制代码

多端同步器.lua 文件源码:
  1. --本代码是直接复制 打开一个websocket连接 ,修改IP和端口即可
  2. local ws = nil
  3. -- 服务器ip端口
  4. address = '192.168.0.101:8000'

  5. nodeTab = {
  6. 应用市场={bounds={l=786,t=275,r=1032,b=533},class="android.widget.TextView",text="华为应用市场"},
  7. 搜索={bounds={l=156,t=264,r=1008,b=384},class="android.widget.TextSwitcher",index="1",level="16"},
  8. 输入框={bounds={l=276,t=96,r=851,b=216},class="android.widget.EditText",index="1",level="13"}
  9. }

  10. function findNode(class,click,findName)
  11.     nodeLib.updateNode()
  12.     -- 找节点
  13.     local ret = nodeLib.findOne(class,true)
  14.     if ret ~= nil then
  15.         -- 找到点击
  16.         print('找到节点了')
  17.         if click then
  18.             nodeLib.click(ret)
  19.         end
  20.         
  21.     end
  22.     sleep(2000)
  23.     -- 找到写入文字
  24.     if findName then
  25.         
  26.          local ret1 = nodeLib.findOne(nodeTab['输入框'],true)
  27.          nodeLib.setText(ret1,findName)
  28.         
  29.     end
  30.    
  31. end



  32. function json(str)
  33.    
  34.     return jsonLib.decode(splitStr(str,"消息:")[2])
  35. end

  36. function wLog(text)
  37.     print(text)
  38.     toast(text,0,0,12)
  39.    
  40. end

  41. function onOpened(handle)
  42.     ws = handle
  43.     print("连接上服务器")
  44.     --sendWebSocket(ws,'服务器1')
  45.    
  46. end

  47. function reConnect()
  48.     sleep(2000)
  49.     wLog("断开连接,3秒后重连")
  50.     setTimer(function()
  51.         ws = nil
  52.         startWebSocket(string.format("ws://%s",address),
  53.         onOpened,onClosed,onError,onRecv)
  54.     end,3000)
  55. end

  56. function onClosed(handle)
  57.     reConnect()
  58. end

  59. function onError(handle)
  60.     reConnect()
  61. end

  62. function onRecv(handle,message)
  63.     print('服务器返回数据:',message)
  64.     wLog(message)
  65.     -- 可在这里实现对应操作
  66.     local res = jsonLib.decode(message)
  67.    
  68.     if res['code']==200 then
  69.         --print('执行点击懒人精灵')
  70.         --tap(659,377)
  71.         toast(res['data'],0,0,30)
  72.         local count = 1
  73.         -- 接收websocket 数据后 执行对应操作
  74.         findNode(nodeTab['应用市场'],true)
  75.         sleep(3000)
  76.         findNode(nodeTab['搜索'],true,'抖音')
  77.         
  78.         
  79.         --findNode(nodeTab[],click,findName)
  80.         
  81.         
  82.         
  83.     end
  84. end

  85. local handle = startWebSocket(string.format("ws://%s",address),
  86. onOpened,onClosed,onError,onRecv)






  87. if handle ~= nil then
  88.    
  89.     while true do
  90.         --[===[local x=-1 y=-1
  91.         ret,x,y = findPic(267,283,582,526,"雷电.png","101010",0,0.98)
  92.         print(ret,x,y)
  93.         if x >= 1 and y >= 1 and sendData ~= '准备' then
  94.         sendWebSocket(ws,'服务器1&准备')
  95.         sendData = '准备'
  96.         end]===]
  97.         sleep(1000)
  98.     end
  99.    
  100.    
  101. end
复制代码
运行结果图片:
websock网页给手机发送数据.mp4_20220512_223005879.jpg



回复

使用道具 举报

zipo_1 | 2022-5-12 23:10:59 | 显示全部楼层
感谢分享
回复

使用道具 举报

qq328280348 | 2022-5-13 09:47:48 | 显示全部楼层
66666666666666
回复

使用道具 举报

jzl6668 | 2022-5-19 19:27:27 | 显示全部楼层


谢谢大佬分享 !
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则