#!/usr/bin/env python
#-*- coding: utf8 -*-
import pygame, gc
from pygame.locals import *
from twisted.protocols.basic import LineOnlyReceiver
from twisted.internet.protocol import ReconnectingClientFactory
from twisted.internet import reactor, threads
def pygame_loop():
pygame.init()
pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
reactor.stop()
return
elif event.type == KEYDOWN and event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
elif event.type == KEYDOWN and event.key == K_SPACE:
print "space"
# send something
pass
class MyProtocol(LineOnlyReceiver):
def connectionMade(self):
print "connectionMade"
# print gc.get_referrers(self)
def lineReceived(self, line):
print line
class MyFactory(ReconnectingClientFactory):
protocol = MyProtocol
def clientConnectionFailed(self, connector, reason):
print "Connection failed - goodbye!"
reactor.stop()
def clientConnectionLost(self, connector, reason):
print "Connection lost - goodbye!"
reactor.stop()
threads.deferToThread(pygame_loop)
factory = MyFactory()
reactor.connectTCP("78.85.128.251", 1915, factory)
reactor.run()