V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
smallgoogle
V2EX  ›  Python

websocket-client 无法外部调用发送事件

  •  
  •   smallgoogle · 2019-01-09 22:17:20 +08:00 · 2137 次点击
    这是一个创建于 1904 天前的主题,其中的信息可能已经有所发展或是发生改变。

    A 内容

    # -*- coding:utf-8 -*-
    import websocket 
    import requests
    import re
    import json
    import time
    
    
    def on_message(ws, message):  # 服务器有数据更新时,主动推送过来的数据
        # message = json.loads(message)
        print(message)
    
    
    def on_error(ws, error):  # 程序报错时,就会触发 on_error 事件
        # print(error)
        time.sleep(10)  # 延时十秒,预防假死
        Start() # 重连
    
    
    def on_close(ws):
        print("Connection closed ……")
    
    
    def on_open(ws):  # 连接到服务器之后就会触发 on_open 事件,这里用于 send 数据
        # print(req)
        ws.send(1)
    
    
    
    def Start():
        # websocket.enableTrace(True)  # 调试开启和关闭
        ws = websocket.WebSocketApp("ws://192.168.31.100:9393", on_message=on_message, on_error=on_error, on_close=on_close)
        ws.on_open = on_open
        ws.run_forever(ping_timeout=30)
    
    
    if __name__ == "__main__":
        Start()
    
    

    我的 websocket 客户端代码是酱紫的;
    那么问题来了,比如是有一另外一个 py 叫 B;
    我如何在 B 里面调用 A 的 websocket 发送内容?

    ws.send('字符串') 是发送内容;
    我如何在外部进行调用 然后发送呢?
    因为我只想在我需要的时候主动发送某些内容给服务器;
    而不是一直等待 on_message 的响应后才发送;

    7 条回复    2019-01-14 10:51:35 +08:00
    E520
        1
    E520  
       2019-01-10 11:23:07 +08:00
    # -*- coding:utf-8 -*-
    import websocket
    import requests
    import re
    import json
    import time,threading


    def on_message(ws, message): # 服务器有数据更新时,主动推送过来的数据
    # message = json.loads(message)
    print(message)


    def on_error(ws, error): # 程序报错时,就会触发 on_error 事件
    # print(error)
    time.sleep(10) # 延时十秒,预防假死
    Start() # 重连


    def on_close(ws):
    print("Connection closed ……")


    def on_open(ws): # 连接到服务器之后就会触发 on_open 事件,这里用于 send 数据
    # print(req)
    ws.send(1)

    def get_msg():
    return '内容'

    def send_msg(ws):
    msg=get_msg()
    ws.send(msg)



    def Start():
    # websocket.enableTrace(True) # 调试开启和关闭
    ws = websocket.WebSocketApp("ws://192.168.31.100:9393", on_message=on_message, on_error=on_error, on_close=on_close)
    ws.on_open = on_open
    threading.Thread(target=send_msg,args=(ws,)).start()
    ws.run_forever(ping_timeout=30)


    if __name__ == "__main__":
    Start()
    E520
        2
    E520  
       2019-01-10 11:23:31 +08:00   ❤️ 1
    加一个线程 传递 Ws 对象即可
    smallgoogle
        3
    smallgoogle  
    OP
       2019-01-10 13:19:24 +08:00
    ![]( )

    @E520 大概好像不行哎。。
    smallgoogle
        4
    smallgoogle  
    OP
       2019-01-10 13:24:10 +08:00
    @E520 不对。好像是可以的。好像是哪里有点问题。说不上来。
    smallgoogle
        5
    smallgoogle  
    OP
       2019-01-10 13:27:06 +08:00
    @E520 这加个线程,只是多了一个函数方法,可是如何调用这个方法呢?
    smallgoogle
        6
    smallgoogle  
    OP
       2019-01-10 13:30:44 +08:00
    @E520 比如怎么调用 send_msg('字符串内容')
    smallgoogle
        7
    smallgoogle  
    OP
       2019-01-14 10:51:35 +08:00
    @E520 前辈你别消失了啊。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3317 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 11:24 · PVG 19:24 · LAX 04:24 · JFK 07:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.