1、安裝 dwebsocket
(venv) c:\code_object\websockettest>pip install dwebsocket -i
2、當前專案環境
python版本
1 (venv) c:\code_object\websockettest>python --version
2 python 3.4.4
django版本
1 (venv) c:\code_object\websockettest>pip list dwebsocket
2 django (1.10)
3 dwebsocket (0.5.5)
4 pip (9.0.1)
5 setuptools (28.8.0)
6 six (1.11.0)
3、相關**
12 @require_websocket # 只接受websocket請求,不接受http請求,這是呼叫了dwebsocket的裝飾器
13 def websocket_test(request):
14 message = request.websocket.wait()
15 request.websocket.send(message)
16 17
18 @accept_websocket # 既能接受http也能接受websocket請求
19 def echo(request):
20 if not request.is_websocket():
21 try:
22 print('---- request.get 資料:--->>',request.get)
27 print('---- 報錯: e--->>',e)
28 return render(request,'test_websocket/user2.html')
29 30 else:
31 redis_my_key = ''
32 while true:
33 # print(dir(request.websocket))
34 # print('request.websocket.count_messages() -->', request.websocket.count_messages())
35 if request.websocket.count_messages() > 0:
36 for message in request.websocket:
37 38 print('request.websocket._get_new_messages() -->', request.websocket._get_new_messages())
39 if request.websocket.is_closed():
40 print('連線關閉')
41 return httpresponse('連線斷開')
42 else:
43 44 # print('request.websocket.is_closed() -->', request.websocket.is_closed())
45 print('--- request.is_websocket() 資料: --->>',message)
46 47 # 將資料寫入資料庫
48 data = json.loads(message.decode())
49 conn_type = data.get('type')
50 my_uuid = data.get('my_uuid')
51 your_uuid = data.get('your_uuid')
52 msg = data.get('message')
53 redis_my_key = 'message_'.format(uuid=my_uuid)
54 redis_you_key = 'message_'.format(uuid=your_uuid)
55 56 if conn_type == 'register':
57 if my_uuid and your_uuid:
58 request.websocket.send("註冊成功".encode('utf-8'))
59 else:
60 request.websocket.send("uuid為空,鏈結斷開".encode('utf-8'))
61 # request.websocket.close()
62 return httpresponse('uuid為空,連線斷開')
63 elif conn_type == 'sendmsg':
64 rc.lpush(redis_my_key, msg)
65 rc.lpush(redis_you_key, msg)
66 67 break
68 elif redis_my_key:
69 data = rc.rpop(redis_my_key)
70 if data:
71 print('收到訊息,立馬傳送data -->', data)
72 request.websocket.send(data.encode('utf-8'))
73 74 # print(dir(request.websocket))
75 # request.websocket.send(message + '這是您發來的 @@@ '.encode('utf-8'))
1
2 362 63
64 65 輸入自己的id: 66
67 68 傳送給誰的id: 69
70 71 連線 websocket
72 傳送 message
73 關閉 websocket
74
75 76
77
78
79
zookeeper Windows安裝及配置
配置環境變數 解壓至e zookeeper zookeeper 3.4.6在e zookeeper 目錄下新建 data和log兩個目錄。進入e zookeeper zookeeper 3.4.6 conf,複製 zoo sample.cfg 並貼上到當前目錄下,命名zoo.cfg。修改datadi...
Python Selenium安裝及環境配置
一 python安裝 二 selenium安裝 兩種安裝方法,第一通過pip python3.x安裝後就預設就會有pip pip.exe預設在python的scripts路徑下 使用pip安裝selenium pip install selenium 後面可以加等號指定selenium的版本 如 p...
jupyter notebook 的安裝及配置
2.安裝jupyter 3.執行jupyter 4.配置jupyter的預設開啟目錄 5.快速啟動jupyter python官網 pip是乙個包管理工具,安裝python之後,pip已經安裝好了 在cmd中輸入以下 python m pip install upgrade pippip insta...