OSDN Git Service

数据包使用JSON
authortanyuliang <tanyuliang2@gmail.com>
Thu, 23 Jul 2015 12:41:42 +0000 (20:41 +0800)
committertanyuliang <tanyuliang2@gmail.com>
Thu, 23 Jul 2015 12:41:42 +0000 (20:41 +0800)
Talk.py
Test.xml [new file with mode: 0644]
tyl.xml [new file with mode: 0644]

diff --git a/Talk.py b/Talk.py
index 0af89b1..5bd2c8e 100644 (file)
--- a/Talk.py
+++ b/Talk.py
@@ -3,7 +3,7 @@ import wx
 import datetime
 import redis
 import thread
-import json
+import simplejson as json
 from time import sleep
 class myapp(wx.App):
     def __init__(self,user_name,un):
@@ -13,8 +13,6 @@ class myapp(wx.App):
         self.bkg = wx.Panel(frame)
         global un_g
         un_g=un
-        global user_name_g
-        user_name_g=user_name
         self.tshow = wx.TextCtrl(self.bkg,style = wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY)
         self.tinput = wx.TextCtrl(self.bkg)
         self.bt = wx.Button(self.bkg,label = "Send")      
@@ -37,9 +35,15 @@ class myapp(wx.App):
         self.tshow.AppendText(self.tinput.GetValue() + "\n")
         rc = redis.Redis(host='pub-redis-19834.us-east-1-4.5.ec2.garantiadata.com',port=19834,password='22842218')
         ps = rc.pubsub()
-        ps.subscribe([user_name_g])
-        user = un_g+self.tinput.GetValue() 
-        rc.publish(user_name_g, user)
+        ps.subscribe([username])
+        #user = un_g+self.tinput.GetValue() 
+        send_dic = {
+         'type': 'p2pchat-in-line',
+         'user': username,
+         'content': self.tinput.GetValue() 
+       }
+        user = json.dumps(send_dic)
+        rc.publish(username, user)
         self.tinput.SetValue("")
     def receive(self):
        rd = redis.Redis(host='pub-redis-19834.us-east-1-4.5.ec2.garantiadata.com',port=19834,password='22842218')
@@ -48,16 +52,15 @@ class myapp(wx.App):
        ps.subscribe([un_g])
        for item in ps.listen():
           if item['type'] == 'message':
-               un_t=item['data'].find(user_name_g)
-               if un_t==0:
-                   now = datetime.datetime.now()
-                   self.tshow.SetDefaultStyle(wx.TextAttr("BLUE"))
-                   wx.CallAfter(self.tshow.AppendText, "User:"+now.strftime('%Y-%m-%d %H:%M:%S')+"\n")
-                   #self.tshow.AppendText("User:"+now.strftime('%Y-%m-%d %H:%M:%S')+"\n")
-                   sleep(0.5)
-                   self.tshow.SetDefaultStyle(wx.TextAttr("BLACK"))
-                   wx.CallAfter(self.tshow.AppendText, item['data'].lstrip(user_name_g) + "\n")
-                   #self.tshow.AppendText(item['data'].lstrip(user_name_g) + "\n")
+               text_json= json.loads(item['data'])
+               if text_json['type'] == 'p2pchat-in-line':
+                   if text_json['user'] == un_g: 
+                       now = datetime.datetime.now()
+                       self.tshow.SetDefaultStyle(wx.TextAttr("BLUE"))
+                       wx.CallAfter(self.tshow.AppendText, "User:"+now.strftime('%Y-%m-%d %H:%M:%S')+"\n")
+                       sleep(0.5)
+                       self.tshow.SetDefaultStyle(wx.TextAttr("BLACK"))
+                       wx.CallAfter(self.tshow.AppendText, text_json['content'] + "\n")
 
 #if __name__ == '__main__':
     #app = myapp()
diff --git a/Test.xml b/Test.xml
new file mode 100644 (file)
index 0000000..8da4782
--- /dev/null
+++ b/Test.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="utf-8"?>
+<root>
+<login passwd='f17ec4800201595a70b2508df1a9772e5914682f0bdf838ef7547a56ae36ad23'>
+</login>
+<family>
+ <person age="18">
+    <name>hzj</name>
+ </person>
+ <person age="19" des="hello">
+    <name>kiki</name>
+ </person>
+ <person age="12">
+    <name>tyl</name>
+ </person>
+ <person age="40">
+    <name>Test</name>
+ </person>
+</family>
+<friend>
+ <person age="20">
+    <name>Linus</name>
+ </person>
+ <person age="21" des="hello">
+    <name>MIUI</name>
+ </person>
+ <person age="22">
+    <name>BSD</name>
+</person>
+ </friend>
+<teacher>
+ <person age="40">
+    <name>Miss Liang</name>
+ </person>
+ <person age="50">
+    <name>Miss Liu</name>
+ </person>
+ <person age="40">
+    <name>Miss She</name>
+ </person>
+</teacher>
+</root>
\ No newline at end of file
diff --git a/tyl.xml b/tyl.xml
new file mode 100644 (file)
index 0000000..8da4782
--- /dev/null
+++ b/tyl.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="utf-8"?>
+<root>
+<login passwd='f17ec4800201595a70b2508df1a9772e5914682f0bdf838ef7547a56ae36ad23'>
+</login>
+<family>
+ <person age="18">
+    <name>hzj</name>
+ </person>
+ <person age="19" des="hello">
+    <name>kiki</name>
+ </person>
+ <person age="12">
+    <name>tyl</name>
+ </person>
+ <person age="40">
+    <name>Test</name>
+ </person>
+</family>
+<friend>
+ <person age="20">
+    <name>Linus</name>
+ </person>
+ <person age="21" des="hello">
+    <name>MIUI</name>
+ </person>
+ <person age="22">
+    <name>BSD</name>
+</person>
+ </friend>
+<teacher>
+ <person age="40">
+    <name>Miss Liang</name>
+ </person>
+ <person age="50">
+    <name>Miss Liu</name>
+ </person>
+ <person age="40">
+    <name>Miss She</name>
+ </person>
+</teacher>
+</root>
\ No newline at end of file