twisted实现broardcast消息

一下是聊天服务端代码:(实现广播)
  1. #! /usr/bin/env python
  2. #coding=utf-8

  3. from twisted.internet import protocol
  4. from twisted.protocols import basic
  5. from twisted.python import log
  6. from twisted.internet import reactor
  7. import sys

  8. class ConfigServer(basic.LineReceiver):
  9.     def __init__(self):
  10.         pass

  11.     def lineReceived(self, line):
  12.         if line == 'quit':
  13.             self.sendLine("Goodbye.")
  14.             self.transport.loseConnection()
  15.         else:
  16.             self.broadcast(line)
  17.     
  18.     def broadcast(self, msg):
  19.         for client in self.factory.clients:
  20.             client.sendLine("someone said: %s" % msg);

  21.     def connectionMade(self):
  22.         self.factory.clients.append(self)
  23.         print "Connect from %s.." % self.transport.getHost()
  24.         self.sendLine("Welcome...%s" % self.transport.getHost())

  25.     def connectionLost(self, reason):
  26.         self.factory.clients.remove(self)
  27.         # self.sendLine("Disconnect...%s" % self.transport.getHost())
  28.         pass

  29. class ConfigServerFactory(protocol.ServerFactory):
  30.     protocol = ConfigServer
  31.     clients = []

  32. def main():
  33.     log.startLogging(sys.stdout)
  34.     reactor.listenTCP(8080,ConfigServerFactory())
  35.     reactor.run()
  36.     
  37. if __name__ == '__main__': 
  38.     main()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Autobahn 中,WebSocket 客户端和服务器端都可以通过 send 方法来发送消息,具体方法如下: ```python class MyServerProtocol(WebSocketServerProtocol): def onMessage(self, payload, isBinary): if isBinary: print("Binary message received: {} bytes".format(len(payload))) else: print("Text message received: {}".format(payload.decode('utf8'))) # 发送消息 self.sendMessage(payload, isBinary=isBinary) ``` 在这个示例中,当服务器收到客户端发送的消息时,会通过 sendMessage 方法将消息发送回客户端。sendMessage 方法的第一个参数是消息的内容,第二个参数指示消息是否是二进制消息。 类似地,WebSocket 客户端也可以通过 send 方法来发送消息,具体方法如下: ```python from twisted.internet import reactor from autobahn.twisted.websocket import WebSocketClientFactory, WebSocketClientProtocol class MyClientProtocol(WebSocketClientProtocol): def onOpen(self): print("WebSocket connection opened") # 发送消息 self.sendMessage("Hello, world!".encode('utf8')) def onMessage(self, payload, isBinary): if isBinary: print("Binary message received: {} bytes".format(len(payload))) else: print("Text message received: {}".format(payload.decode('utf8'))) if __name__ == '__main__': factory = WebSocketClientFactory() factory.protocol = MyClientProtocol reactor.connectTCP("localhost", 9000, factory) print("WebSocket client connected to server") reactor.run() ``` 在这个示例中,当客户端连接到服务器时,会调用 onOpen 方法,在这个方法中可以发送消息。在这个示例中,发送了一个文本消息。在 onMessage 方法中,处理服务器发送的消息,这里只是简单地输出收到的消息

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值