rabin 7 年之前
父節點
當前提交
4e0df6c252
共有 1 個文件被更改,包括 22 次插入20 次删除
  1. 22 20
      tcp/server.py

+ 22 - 20
tcp/server.py

@@ -9,39 +9,41 @@ from demeter import *
 from tornado.tcpserver import TCPServer
 from tornado.tcpserver import TCPServer
 from tornado.ioloop  import IOLoop
 from tornado.ioloop  import IOLoop
 
 
-class Connection(object):	
+class Connection(object):
-	clients = set()	
+	clients = set()
-	def __init__(self, stream, address):   
+	def __init__(self, stream, address):
-		Connection.clients.add(self)   
+		Connection.clients.add(self)
-		self._stream = stream	
+		self._stream = stream
-		self._address = address	
+		self._address = address
-		self._stream.set_close_callback(self.on_close)	
+		self._stream.set_close_callback(self.on_close)
-		self.read_message()	
+		self.read_message()
-		print "A new user has entered the chat room.", address   
 		
 		
 	def read_message(self):
 	def read_message(self):
-		self._stream.read_until('\n', self.broadcast_messages)	
+		self._stream.read_until('\n', self.broadcast_messages)
 	
 	
 	def broadcast_messages(self, data):
 	def broadcast_messages(self, data):
 		pub = Pub()
 		pub = Pub()
-		key = ''
+		temp = data.split(':')
-		value = ''
+		key = temp[0]
+		value = temp[1]
 		pub.push(key, value)
 		pub.push(key, value)
 		
 		
-		print "User said:", data[:-1], self._address  
+		#print "User said:", data[:-1], self._address
-		for conn in Connection.clients:	
+		"""
+		for conn in Connection.clients:
 			conn.send_message(data)
 			conn.send_message(data)
+		"""
 		self.read_message()
 		self.read_message()
 		
 		
 	def send_message(self, data):
 	def send_message(self, data):
 		self._stream.write(data)
 		self._stream.write(data)
 			
 			
-	def on_close(self):	
+	def on_close(self):
-		print "A user has left the chat room.", self._address  
+		#print "A user has left the chat room.", self._address
 		Connection.clients.remove(self)
 		Connection.clients.remove(self)
 	
 	
 class Server(TCPServer):	
 class Server(TCPServer):	
-	def handle_stream(self, stream, address):   
+	def handle_stream(self, stream, address):
-		#print "New connection :", address, stream   
+		#print "New connection :", address, stream
-		Connection(stream, address)   
+		Connection(stream, address)
-		#print "connection num is:", len(Connection.clients)  
+		#print "connection num is:", len(Connection.clients)