【问题标题】:How to implement pub/sub socket.io client in python?如何在 python 中实现 pub/sub socket.io 客户端?
【发布时间】:2015-05-20 15:05:52
【问题描述】:

我一直在尝试实现以下代码的 python 等效项,但似乎无法使其正常工作。

var io = require('socket.io-client')
var socket = io('http://devel.hz.udoidio.info:5000')

socket.on('connect', function () {
  socket.emit('subscribe', 'new-tx')
})

socket.on('new-tx', function (txid) {
  console.log('New tx: ' + txid)
})

我尝试了这种方法,但似乎没有任何效果。

from socketIO_client import SocketIO, LoggingNamespace

def on_response(*args):
    print 'on_response', args

baseurl = "http://v1.livenet.bitcoin.chromanode.net"
socketIO = SocketIO(baseurl, 80, LoggingNamespace)
socketIO.on('subscribe', on_response)
socketIO.emit('subscribe', 'new-block')
socketIO.wait()

【问题讨论】:

    标签: python socket.io publish-subscribe


    【解决方案1】:

    我解决了这个问题,下面是正确的解决方案。

    from socketIO_client import SocketIO
    
    def on_response(*args):
        print 'on_response', args
    
    baseurl = "http://devel.hz.udoidio.info"
    with SocketIO(baseurl, 5000) as socketIO:
        socketIO.on('new-tx', on_response)
        socketIO.emit('subscribe', 'new-tx')
        socketIO.wait()
    

    【讨论】:

      猜你喜欢
      • 2012-10-13
      • 1970-01-01
      • 2021-04-21
      • 2021-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多