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
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
61 additions
and
41 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,48 +154,66 @@ class Server ():
# 开启线程记录
if
raw_str
:
t1
=
threading
.
Thread
(
target
=
self
.
send_data
,
args
=
[
raw_str
,
address
]
)
t1
.
start
()
def
send_data
(
self
,
data
,
address
):
import
struct
from
urllib
import
unquote
try
:
username
=
unquote
(
self
.
users
[
address
]
)
except
:
username
=
'匿名用户'
#if data:
# data = str ( '【' + username + '说】' + data )
#else:
# return False
token
=
"
\x81
"
length
=
len
(
data
)
if
length
<
126
:
print
'126'
token
+=
struct
.
pack
(
"B"
,
length
)
elif
length
<=
0xFFFF
:
print
'0xFFFF'
token
+=
struct
.
pack
(
"!BH"
,
126
,
length
)
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:
# data = str ( '【' + username + '说】' + data )
# else:
# return False
token
=
"
\x81
"
length
=
len
(
str_data
)
if
length
<
126
:
print
'126'
token
+=
struct
.
pack
(
"B"
,
length
)
elif
length
<=
0xFFFF
:
print
'0xFFFF'
token
+=
struct
.
pack
(
"!BH"
,
126
,
length
)
else
:
print
'!BQ'
token
+=
struct
.
pack
(
"!BQ"
,
127
,
length
)
print
'token=
%
s'
%
token
# struct为Python中处理二进制数的模块,二进制流为C,或网络流的形式。
str_data
=
'
%
s
%
s'
%
(
token
,
str_data
)
print
str_data
try
:
# for key, val in self.clients.iteritems ():
# print
# client = val
# try:
# print 'sned=%s' % data
# client.send(data)
# except:
# self.close_client ( key )
client
=
self
.
clients
[
address
]
client
.
send
(
str_data
)
except
:
pass
else
:
pass
else
:
pass
else
:
print
'!BQ'
token
+=
struct
.
pack
(
"!BQ"
,
127
,
length
)
print
'token=
%
s'
%
token
# struct为Python中处理二进制数的模块,二进制流为C,或网络流的形式。
data
=
'
%
s
%
s'
%
(
token
,
data
)
print
data
try
:
#for key, val in self.clients.iteritems ():
# print
# client = val
# try:
# print 'sned=%s' % data
# client.send(data)
# except:
# self.close_client ( key )
client
=
self
.
clients
[
address
]
client
.
send
(
data
)
except
:
pass
def
close_client
(
self
,
address
):
...
...
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