rabin 7 years ago
parent
commit
f4dd99024b
2 changed files with 30 additions and 15 deletions
  1. 15 0
      tcp.py
  2. 15 15
      tcp/server.py

+ 15 - 0
tcp.py

@@ -0,0 +1,15 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+    demeter web
+    name:tcp.py
+    author:rabin
+"""
+from tcp.server import *
+from mqtt.pub import *
+
+if __name__ == '__main__':	
+	print "Server start ......"	
+	server = Server()
+	server.listen(8000)
+	IOLoop.instance().start()

+ 15 - 15
tcp/server.py

@@ -5,6 +5,7 @@
 	name:server.py
 	author:rabin
 """
+from demeter import *
 from tornado.tcpserver import TCPServer
 from tornado.ioloop  import IOLoop
 
@@ -18,30 +19,29 @@ class Connection(object):
 		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)	
 	
-	def broadcast_messages(self, data):	
+	def broadcast_messages(self, data):
+		pub = Pub()
+		key = ''
+		value = ''
+		pub.push(key, value)
+		
 		print "User said:", data[:-1], self._address  
 		for conn in Connection.clients:	
-			conn.send_message(data)	
-		self.read_message()	
+			conn.send_message(data)
+		self.read_message()
 		
-	def send_message(self, data):	
-		self._stream.write(data)   
+	def send_message(self, data):
+		self._stream.write(data)
 			
 	def on_close(self):	
 		print "A user has left the chat room.", self._address  
 		Connection.clients.remove(self)
 	
-class ChatServer(TCPServer):	
+class Server(TCPServer):	
 	def handle_stream(self, stream, address):   
-		print "New connection :", address, stream   
+		#print "New connection :", address, stream   
 		Connection(stream, address)   
-		print "connection num is:", len(Connection.clients)  
-	
-if __name__ == '__main__':	
-	print "Server start ......"	
-	server = ChatServer()
-	server.listen(8000)
-	IOLoop.instance().start()
+		#print "connection num is:", len(Connection.clients)