-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.js
More file actions
82 lines (60 loc) · 2.34 KB
/
test.js
File metadata and controls
82 lines (60 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var crypto = require('crypto');
var path = require("path"),
pb = require("protobufjs"),
net = require("net");
//Initialize from .proto file
var builder = pb.loadProtoFile(path.join(__dirname, "", "IM.Login.proto")),
IMLoginReq = builder.build("IM.Login.IMLoginReq"),
IMLoginRes = builder.build("IM.Login.IMLoginRes"),
UserStatType = builder.build("IM.BaseDefine.UserStatType"),
UserInfo = builder.build("IM.BaseDefine.UserInfo"),
ResultType = builder.build("IM.BaseDefine.ResultType"),
ClientType = builder.build("IM.BaseDefine.ClientType");
var md5 = crypto.createHash('md5');
md5.update("pl,okm123");
var msg = new IMLoginReq("test", md5.digest('hex'), UserStatType.USER_STATUS_ONLINE, ClientType.CLIENT_TYPE_WINDOWS, "win_10086");
console.log(msg);
var client = new net.Socket();
client.connect(8000, "172.18.0.236", function() {
console.log('CONNECTED'); // 建立连接后立即向服务器发送数据,服务器将收到这些数据
var buf = new Buffer(16 + msg.toBuffer().length);
buf.fill(0);
buf.writeInt32BE(buf.length);
buf.writeInt16BE(1, 4);
buf.writeInt16BE(0, 6);
buf.writeInt16BE(1, 8);
buf.writeInt16BE(259, 10);
buf.writeInt16BE(1, 12);
buf.writeInt16BE(0, 14);
/*
CByteStream::WriteInt32(m_pHeaderBuff, m_length);
CByteStream::WriteUint16(m_pHeaderBuff + 4, m_version);
CByteStream::WriteUint16(m_pHeaderBuff + 6, m_flag);
CByteStream::WriteUint16(m_pHeaderBuff + 8, m_moduleId);1
CByteStream::WriteUint16(m_pHeaderBuff + 10, m_commandId);259
CByteStream::WriteUint16(m_pHeaderBuff + 12, m_seqNumber);1
CByteStream::WriteUint16(m_pHeaderBuff + 14, m_reserved);
*/
console.log(buf);
/*UInt32 length = imcore::HEADER_LENGTH + pbBody->ByteSize();
m_TTPBHeader.setLength(length);
std::unique_ptr<byte> data(new byte[length]);
memset(data.get(), 0, length);
memcpy(data.get(), m_TTPBHeader.getSerializeBuffer(), imcore::HEADER_LENGTH);
if (!pbBody->SerializeToArray(data.get() + imcore::HEADER_LENGTH, pbBody->ByteSize()))
{
LOG__(ERR, _T("pbBody SerializeToArray failed"));
return;
}
*/
msg.toBuffer().copy(buf, 16);
client.write(buf);
});
client.on('data', function(data) {
var buf = new Buffer(data.length - 16);
data.copy(buf, 0, 16);
var res = IMLoginRes.decode(buf);
console.log(res);
client.destroy();});
client.on('close', function() {
console.log('Connection closed');});