melo-example-robot 1.7.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,93 @@
1
+ import * as melo from './MeloForEgret';
2
+ import { Actor } from '@bigtyphoon/melo-robot';
3
+
4
+
5
+ export class Client {
6
+ constructor(private actor: Actor) {
7
+
8
+ }
9
+
10
+ openid = String(Math.round(Math.random() * 1000));
11
+
12
+ meloClient = new melo.WSClient();
13
+
14
+ public connectGate(): void {
15
+
16
+ let host = '127.0.0.1';
17
+ let port = '3014';
18
+ this.meloClient.on(melo.WSClient.EVENT_IO_ERROR, function(event) {
19
+ // 错误处理
20
+ console.error('error', event);
21
+ });
22
+ this.meloClient.on(melo.WSClient.EVENT_CLOSE, function(event) {
23
+ // 关闭处理
24
+ console.error('close', event);
25
+ });
26
+ this.meloClient.on(melo.WSClient.EVENT_HEART_BEAT_TIMEOUT, function(event) {
27
+ // 心跳timeout
28
+ console.error('heart beat timeout', event);
29
+ });
30
+ this.meloClient.on(melo.WSClient.EVENT_KICK, function(event) {
31
+ // 踢出
32
+ console.error('kick', event);
33
+ });
34
+
35
+ // this.actor.emit("incr" , "gateConnReq");
36
+ this.actor.emit('start' , 'gateConn' , this.actor.id);
37
+ this.meloClient.init({
38
+ host: host,
39
+ port: port
40
+ }, () => {
41
+ this.actor.emit('end' , 'gateConn' , this.actor.id);
42
+ // 连接成功执行函数
43
+ console.log('gate连接成功');
44
+
45
+
46
+ this.gateQuery();
47
+ });
48
+ }
49
+ gateQuery() {
50
+ // this.actor.emit("incr" , "gateQueryReq");
51
+ this.actor.emit('start' , 'gateQuery' , this.actor.id);
52
+ this.meloClient.request('gate.gateHandler.queryEntry', {uid: this.openid} , (result: {code: number , host: string , port: number}) => {
53
+ // 消息回调
54
+ // console.log("gate返回",JSON.stringify(result));
55
+ this.actor.emit('end' , 'gateQuery' , this.actor.id);
56
+ this.meloClient.disconnect();
57
+ this.connectToConnector(result);
58
+ });
59
+ }
60
+
61
+ connectToConnector(result: {host: string , port: number}) {
62
+ // this.actor.emit("incr" , "loginConnReq");
63
+ this.actor.emit('start' , 'loginConn' , this.actor.id);
64
+ this.meloClient.init({
65
+ host: result.host,
66
+ port: result.port
67
+ }, () => {
68
+ this.actor.emit('end' , 'loginConn' , this.actor.id);
69
+ // 连接成功执行函数
70
+ console.log('connector连接成功');
71
+
72
+ this.loginQuery({rid: this.actor.id.toString() , username : this.actor.id.toString()});
73
+ });
74
+ }
75
+
76
+ loginQuery(result: {rid: string, username: string}) {
77
+
78
+ // this.actor.emit("incr" , "loginQueryReq");
79
+ this.actor.emit('start' , 'loginQuery' , this.actor.id);
80
+ this.meloClient.request('connector.entryHandler.enter', result , (ret: any) => {
81
+ // 消息回调
82
+ this.actor.emit('end' , 'loginQuery' , this.actor.id);
83
+ console.log('connector返回', JSON.stringify(result));
84
+
85
+ });
86
+ }
87
+ }
88
+
89
+ export default function(actor: Actor) {
90
+ let client = new Client(actor);
91
+ client.connectGate();
92
+ return client;
93
+ }