Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation
This project
Loading...
Sign in
platform
/
tx1_box_serv
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 53936aa9
authored
Dec 25, 2018
by
熊付
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
【websocket】
1 parent
b17c0900
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
11 deletions
code/tx1_box_serv/apps/api/ws_serv.py
code/tx1_box_serv/apps/api/ws_serv.py
View file @
53936aa
...
...
@@ -37,9 +37,11 @@ class Server ():
self
.
stops
=
[]
self
.
h
=
h
self
.
p
=
p
self
.
subtask_id
=
None
# 监听客户端连接
def
listen_client
(
self
):
while
1
:
time
.
sleep
(
200
)
# 循环监听
tcpClientSock
,
addr
=
self
.
sock
.
accept
()
address
=
addr
[
0
]
+
':'
+
str
(
addr
[
1
]
)
# ip:port
...
...
@@ -152,22 +154,34 @@ class Server ():
# 开启线程记录
if
raw_str
:
t1
=
threading
.
Thread
(
target
=
self
.
send_data
,
args
=
[
raw_str
,
address
]
)
t1
.
start
()
def
send_data
(
self
,
data
,
address
):
print
type
(
raw_str
)
try
:
da
=
eval
(
raw_str
)
if
type
(
da
)
is
dict
:
if
da
.
has_key
(
'subtask_id'
):
self
.
subtask_id
=
da
[
'subtask_id'
]
except
Exception
as
e
:
print
e
def
send_data
(
self
,
str_data
,
address
):
if
type
(
str_data
)
is
str
:
data
=
json
.
loads
(
str_data
)
if
type
(
data
)
is
dict
:
if
data
.
has_key
(
'subtask_id'
):
subtask_id
=
data
[
'subtask_id'
]
if
self
.
subtask_id
and
self
.
subtask_id
==
subtask_id
:
import
struct
from
urllib
import
unquote
try
:
username
=
unquote
(
self
.
users
[
address
]
)
except
:
username
=
'匿名用户'
#
if data:
#
if data:
# data = str ( '【' + username + '说】' + data )
#
else:
#
else:
# return False
token
=
"
\x81
"
length
=
len
(
data
)
length
=
len
(
str_
data
)
if
length
<
126
:
print
'126'
token
+=
struct
.
pack
(
"B"
,
length
)
...
...
@@ -180,10 +194,10 @@ class Server ():
print
'token=
%
s'
%
token
# struct为Python中处理二进制数的模块,二进制流为C,或网络流的形式。
data
=
'
%
s
%
s'
%
(
token
,
data
)
print
data
str_data
=
'
%
s
%
s'
%
(
token
,
str_
data
)
print
str_
data
try
:
#
for key, val in self.clients.iteritems ():
#
for key, val in self.clients.iteritems ():
# print
# client = val
# try:
...
...
@@ -192,9 +206,15 @@ class Server ():
# except:
# self.close_client ( key )
client
=
self
.
clients
[
address
]
client
.
send
(
data
)
client
.
send
(
str_
data
)
except
:
pass
else
:
pass
else
:
pass
else
:
pass
def
close_client
(
self
,
address
):
try
:
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment