import
socket,uuid,time,google.protobuf
import
AuthMsg_pb2,ChatMsg_pb2
skt
=
socket.socket(socket.AF_INET,socket.SOCK_STREAM)
address
=
(
"test.host.com"
,
5000
)
skt.connect(address)
token
=
{
'isAuth'
:
1
,
'nickName'
:
'测试23242'
,
'headUrl'
:
'https://down.host.com/head/userhead.png'
,
'defaultId'
:
'801adfafcc0d1afa0d'
,
'accessToken'
:
'tk:801adfafcc0d1afa0d:adfasdfaec0faf6a9'
,
'token'
:
'1010e3d11973980c36ec8'
}
def
auth():
am
=
AuthMsg_pb2.AuthMsg()
am.token
=
token[
'accessToken'
]
am.proto_class_name
=
"AuthMsg"
am.body.from_id
=
token[
'defaultId'
]
am.body.device_id
=
"861234123412"
am.body.device_type
=
1
;
bnry
=
am.SerializeToString()
skt.send(addNettyHeader(bnry));
def
chat(news):
msgstr
=
ChatMsg_pb2.ChatMsg()
msgstr.token
=
userinfo[
'accessToken'
]
msgstr.proto_class_name
=
"ChatMsg"
msgstr.body.msg_id
=
"100-"
+
str
(uuid.uuid1()).replace(
"-"
,"")
msgstr.body.main_type
=
6
if
(news[
'type'
]
=
=
2
):
msgstr.body.slave_type
=
3
msgstr.body.content
=
news[
'txt'
]
elif
(news[
'type'
]
=
=
1
):
msgstr.body.slave_type
=
1
msgstr.body.content
=
news[
'txt'
]
msgstr.body.from_id
=
userinfo[
'defaultId'
]
msgstr.body.from_name
=
userinfo[
'nickName'
]
msgstr.body.from_head_url
=
userinfo[
'headUrl'
]
msgstr.body.dest_id
=
news[
'to'
]
msgstr.body.send_time
=
int
(time.time()
*
1000
)
msgstr.body.device_type
=
1
msgstr.body.device_slave_type
=
11
bnry
=
msgstr.SerializeToString()
skt.send(addNettyHeader(bnry))
def
addNettyHeader(msg):
value
=
len
(msg)
bits
=
value &
0x7f
value >>
=
7
header_array
=
[]
while
value:
header_array.append(
0x80
| bits)
bits
=
value &
0x7f
value >>
=
7
header_array.append(bits)
return
bytes(header_array)
+
msg
auth()
msg1
=
{
"type"
:
1
,
"txt"
:
"今天天气真好,有约吗?"
,
"to"
:
123412
}
msg2
=
{
"type"
:
3
,
"txt"
:
"https://img.host.com/test.jpg"
,
"to"
:
123412
}
chat(msg1)
chat(msg2)