网游服务器,一般都要做一个踢人(kick)机制。简单说就是类似于QQ账号,后一个登陆者,把前一个登陆者踢掉,服务器与前一个登陆者断开的机制。
查了半天twisted的API,最后发现是在protocol实例里面的transport属性里的loseConnection()方法。也就是调用protocol.transport.loseConnection()即可完成。
附注:
这里的protocol指的是,继承了 twisted.internet.protocol.Protocol 的类的实例。
=======================================
protocol属性
['__doc__', '__implemented__', '__init__', '__module__', '__providedBy__', '__provides__',
'connected', 'connectionLost', 'connectionMade', 'dataReceived',
'factory', 'makeConnection', 'transport']
=======================================
protocol.transport属性
['SEND_LIMIT', 'TLS', '__class__', '__delattr__', '__dict__', '__doc__',
'__format__', '__getattribute__', '__hash__', '__implemented__', '__init__',
'__module__', '__new__', '__providedBy__', '__provides__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'__weakref__', '_closeSocket', '_closeWriteConnection', '_postLoseConnection',
'_socketShutdownMethod', '_tempDataBuffer', '_tempDataLen', '_writeDisconnected',
'_writeDisconnecting',
'bufferSize', 'client', 'connected', 'connectionLost', 'dataBuffer',
'disconnected', 'disconnecting', 'doRead', 'doWrite', 'fileno', 'getHandle',
'getHost', 'getPeer', 'getTcpKeepAlive', 'getTcpNoDelay', 'hostname',
'logPrefix', 'logstr', 'loseConnection', 'loseWriteConnection',
'offset', 'pauseProducing', 'producer', 'producerPaused',
'protocol', 'reactor', 'readConnectionLost', 'registerProducer',
'repstr', 'resumeProducing', 'server', 'sessionno', 'setTcpKeepAlive',
'setTcpNoDelay', 'socket', 'startReading', 'startTLS', 'startWriting',
'stopConsuming', 'stopProducing', 'stopReading', 'stopWriting',
'streamingProducer', 'unregisterProducer', 'write', 'writeConnectionLost',
'writeSequence', 'writeSomeData']
=======================================
protocol.transport.socket属性
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__',
'__weakref__', '_sock',
'accept', 'bind', 'close', 'connect', 'connect_ex', 'dup', 'family',
'fileno', 'getpeername', 'getsockname', 'getsockopt', 'gettimeout',
'listen', 'makefile', 'proto', 'recv', 'recv_into', 'recvfrom',
'recvfrom_into', 'send', 'sendall', 'sendto', 'setblocking',
'setsockopt', 'settimeout', 'shutdown', 'type']
如果要操作twisted的底层socket,那么,请参考protocol.transport.socket属性