2016-12-14 15:30:46 +00:00
|
|
|
# install_twisted_rector must be called before importing the reactor
|
2017-02-27 21:33:28 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2011-09-06 18:52:49 +00:00
|
|
|
from kivy.support import install_twisted_reactor
|
|
|
|
|
2017-02-27 21:33:28 +00:00
|
|
|
install_twisted_reactor()
|
2011-09-06 18:52:49 +00:00
|
|
|
|
2017-02-27 21:33:28 +00:00
|
|
|
# A Simple Client that send messages to the Echo Server
|
2011-09-06 18:52:49 +00:00
|
|
|
from twisted.internet import reactor, protocol
|
|
|
|
|
2015-02-02 01:09:20 +00:00
|
|
|
|
2011-09-06 18:52:49 +00:00
|
|
|
class EchoClient(protocol.Protocol):
|
|
|
|
def connectionMade(self):
|
|
|
|
self.factory.app.on_connection(self.transport)
|
|
|
|
|
|
|
|
def dataReceived(self, data):
|
2017-02-27 21:33:28 +00:00
|
|
|
self.factory.app.print_message(data.decode('utf-8'))
|
2011-09-06 18:52:49 +00:00
|
|
|
|
2015-02-02 01:09:20 +00:00
|
|
|
|
2017-02-27 21:33:28 +00:00
|
|
|
class EchoClientFactory(protocol.ClientFactory):
|
2011-09-06 18:52:49 +00:00
|
|
|
protocol = EchoClient
|
2015-02-02 01:09:20 +00:00
|
|
|
|
2011-09-06 18:52:49 +00:00
|
|
|
def __init__(self, app):
|
|
|
|
self.app = app
|
|
|
|
|
2017-02-27 21:33:28 +00:00
|
|
|
def startedConnecting(self, connector):
|
|
|
|
self.app.print_message('Started to connect.')
|
|
|
|
|
|
|
|
def clientConnectionLost(self, connector, reason):
|
|
|
|
self.app.print_message('Lost connection.')
|
2011-09-06 18:52:49 +00:00
|
|
|
|
2017-02-27 21:33:28 +00:00
|
|
|
def clientConnectionFailed(self, connector, reason):
|
|
|
|
self.app.print_message('Connection failed.')
|
2011-09-06 18:52:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
from kivy.app import App
|
|
|
|
from kivy.uix.label import Label
|
|
|
|
from kivy.uix.textinput import TextInput
|
|
|
|
from kivy.uix.boxlayout import BoxLayout
|
|
|
|
|
2015-02-02 01:09:20 +00:00
|
|
|
|
2011-09-06 18:55:24 +00:00
|
|
|
# A simple kivy App, with a textbox to enter messages, and
|
|
|
|
# a large label to display all the messages received from
|
|
|
|
# the server
|
2011-09-06 18:52:49 +00:00
|
|
|
class TwistedClientApp(App):
|
|
|
|
connection = None
|
2017-02-27 21:33:28 +00:00
|
|
|
textbox = None
|
|
|
|
label = None
|
2011-09-06 18:52:49 +00:00
|
|
|
|
|
|
|
def build(self):
|
|
|
|
root = self.setup_gui()
|
|
|
|
self.connect_to_server()
|
|
|
|
return root
|
|
|
|
|
|
|
|
def setup_gui(self):
|
|
|
|
self.textbox = TextInput(size_hint_y=.1, multiline=False)
|
|
|
|
self.textbox.bind(on_text_validate=self.send_message)
|
|
|
|
self.label = Label(text='connecting...\n')
|
2017-02-27 21:33:28 +00:00
|
|
|
layout = BoxLayout(orientation='vertical')
|
|
|
|
layout.add_widget(self.label)
|
|
|
|
layout.add_widget(self.textbox)
|
|
|
|
return layout
|
2011-09-06 18:52:49 +00:00
|
|
|
|
|
|
|
def connect_to_server(self):
|
2017-02-27 21:33:28 +00:00
|
|
|
reactor.connectTCP('localhost', 8000, EchoClientFactory(self))
|
2011-09-06 18:52:49 +00:00
|
|
|
|
|
|
|
def on_connection(self, connection):
|
2017-02-27 21:33:28 +00:00
|
|
|
self.print_message("Connected successfully!")
|
2011-09-06 18:52:49 +00:00
|
|
|
self.connection = connection
|
|
|
|
|
|
|
|
def send_message(self, *args):
|
|
|
|
msg = self.textbox.text
|
|
|
|
if msg and self.connection:
|
2017-02-27 21:33:28 +00:00
|
|
|
self.connection.write(msg.encode('utf-8'))
|
2011-09-06 18:52:49 +00:00
|
|
|
self.textbox.text = ""
|
|
|
|
|
|
|
|
def print_message(self, msg):
|
2017-02-27 21:33:28 +00:00
|
|
|
self.label.text += "{}\n".format(msg)
|
2011-09-06 18:52:49 +00:00
|
|
|
|
2016-12-17 07:50:52 +00:00
|
|
|
|
2011-09-06 18:52:49 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
TwistedClientApp().run()
|