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 ():
...
@@ -37,9 +37,11 @@ class Server ():
self
.
stops
=
[]
self
.
stops
=
[]
self
.
h
=
h
self
.
h
=
h
self
.
p
=
p
self
.
p
=
p
self
.
subtask_id
=
None
# 监听客户端连接
# 监听客户端连接
def
listen_client
(
self
):
def
listen_client
(
self
):
while
1
:
while
1
:
time
.
sleep
(
200
)
# 循环监听
# 循环监听
tcpClientSock
,
addr
=
self
.
sock
.
accept
()
tcpClientSock
,
addr
=
self
.
sock
.
accept
()
address
=
addr
[
0
]
+
':'
+
str
(
addr
[
1
]
)
# ip:port
address
=
addr
[
0
]
+
':'
+
str
(
addr
[
1
]
)
# ip:port
...
@@ -152,48 +154,66 @@ class Server ():
...
@@ -152,48 +154,66 @@ class Server ():
# 开启线程记录
# 开启线程记录
if
raw_str
:
if
raw_str
:
t1
=
threading
.
Thread
(
target
=
self
.
send_data
,
args
=
[
raw_str
,
address
]
)
print
type
(
raw_str
)
t1
.
start
()
try
:
da
=
eval
(
raw_str
)
def
send_data
(
self
,
data
,
address
):
if
type
(
da
)
is
dict
:
import
struct
if
da
.
has_key
(
'subtask_id'
):
from
urllib
import
unquote
self
.
subtask_id
=
da
[
'subtask_id'
]
try
:
except
Exception
as
e
:
username
=
unquote
(
self
.
users
[
address
]
)
print
e
except
:
username
=
'匿名用户'
def
send_data
(
self
,
str_data
,
address
):
#if data:
if
type
(
str_data
)
is
str
:
# data = str ( '【' + username + '说】' + data )
data
=
json
.
loads
(
str_data
)
#else:
if
type
(
data
)
is
dict
:
# return False
if
data
.
has_key
(
'subtask_id'
):
token
=
"
\x81
"
subtask_id
=
data
[
'subtask_id'
]
length
=
len
(
data
)
if
self
.
subtask_id
and
self
.
subtask_id
==
subtask_id
:
if
length
<
126
:
import
struct
print
'126'
from
urllib
import
unquote
token
+=
struct
.
pack
(
"B"
,
length
)
try
:
elif
length
<=
0xFFFF
:
username
=
unquote
(
self
.
users
[
address
]
)
print
'0xFFFF'
except
:
token
+=
struct
.
pack
(
"!BH"
,
126
,
length
)
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
:
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
pass
def
close_client
(
self
,
address
):
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