コード例 #1
0
ファイル: Client.py プロジェクト: macytter/ktn_prosjekt
class Client:
    """
	This is the chat client class
	"""
    def __init__(self, host, server_port):
        """
		This method is run when creating a new Client object
		"""

        # Set up the socket connection to the server
        self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.host = host
        self.server_port = server_port

        self.userLoggedIn = False

        self.messageReceiver = MessageReceiver(
            self, self.connection)  # is a thread by itself.
        self.userInput = UserInput(self)  # is a thread by itself.

        self.parser = MessageParser(self)

        self.run()

    def run(self):
        # Initiate the connection to the server
        try:
            self.connection.connect((self.host, self.server_port))
        except:
            print "No connection to the server were established. Exiting."
            sys.exit()

        self.messageReceiver.start()  # start the thread
        self.userInput.start()  # start the thread

        print "Client Ready. Please type command. type 'help' to view server commands."

    def disconnect(self):
        self.connection.close()
        print "Server disconnected"
        self.userInput.kill()
        sys.exit()
        pass

    def receive_message(self, message):
        parsed_message = self.parser.parse(message)
        print parsed_message

    def send_payload(self, data):
        json = self.parser.encode(data)
        self.connection.send(json)
        pass
コード例 #2
0
ファイル: Client.py プロジェクト: macytter/ktn_prosjekt
class Client:
	"""
	This is the chat client class
	"""

	def __init__(self, host, server_port):
		"""
		This method is run when creating a new Client object
		"""

		# Set up the socket connection to the server
		self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		self.host = host
		self.server_port = server_port

		self.userLoggedIn = False

		self.messageReceiver = MessageReceiver(self, self.connection)  # is a thread by itself.
		self.userInput = UserInput(self)  # is a thread by itself.

		self.parser = MessageParser(self)

		self.run()

	def run(self):
		# Initiate the connection to the server
		try:
			self.connection.connect((self.host, self.server_port))
		except:
			print "No connection to the server were established. Exiting."
			sys.exit()

		self.messageReceiver.start()  # start the thread
		self.userInput.start()  # start the thread

		print "Client Ready. Please type command. type 'help' to view server commands."

	def disconnect(self):
		self.connection.close()
		print "Server disconnected"
		self.userInput.kill()
		sys.exit()
		pass

	def receive_message(self, message):
		parsed_message = self.parser.parse(message)
		print parsed_message

	def send_payload(self, data):
		json = self.parser.encode(data)
		self.connection.send(json)
		pass