|
1 | 1 | import Ezy from 'ezyfox-server-react-native-client'; |
2 | 2 | import Mvc from 'mvc-es6'; |
3 | | -import {Command, ZONE_NAME, APP_NAME} from "./SocketConstants"; |
| 3 | +import {Command, ZONE_NAME, APP_NAME} from './SocketConstants'; |
4 | 4 |
|
5 | 5 | class SocketProxy { |
| 6 | + static instance = null; |
6 | 7 |
|
7 | | - static instance = null; |
8 | | - |
9 | | - static getInstance() { |
10 | | - if (!SocketProxy.instance) |
11 | | - SocketProxy.instance = new SocketProxy(); |
12 | | - return SocketProxy.instance; |
13 | | - } |
14 | | - |
15 | | - setup() { |
16 | | - let config = new Ezy.ClientConfig(); |
17 | | - config.clientName = ZONE_NAME |
18 | | - config.enableSSL = true; |
19 | | - config.enableDebug = true; |
20 | | - let clients = Ezy.Clients.getInstance(); |
21 | | - clients.newDefaultClient(config, client => { |
22 | | - this.doSetup(client); |
23 | | - }); |
24 | | - } |
25 | | - |
26 | | - doSetup(client) { |
27 | | - let mvc = Mvc.getInstance(); |
28 | | - let models = mvc.models; |
29 | | - let handshakeHandler = new Ezy.HandshakeHandler(); |
30 | | - handshakeHandler.getLoginRequest = function () { |
31 | | - let connection = models.connection; |
32 | | - let username = connection.username; |
33 | | - let password = connection.password; |
34 | | - return [ZONE_NAME, username, password, []]; |
35 | | - }; |
36 | | - |
37 | | - let loginSuccessHandler = new Ezy.LoginSuccessHandler(); |
38 | | - loginSuccessHandler.handleLoginSuccess = function () { |
39 | | - let accessAppRequest = [APP_NAME, []]; |
40 | | - this.client.send(Ezy.Command.APP_ACCESS, accessAppRequest); |
41 | | - }; |
42 | | - |
43 | | - let loginErrorHandler = new Ezy.LoginErrorHandler(); |
44 | | - loginErrorHandler.handleLoginError = function (event) { |
45 | | - let loginController = mvc.getController("login"); |
46 | | - loginController.updateViews("loginError", event[1]); |
47 | | - }; |
48 | | - |
49 | | - let accessAppHandler = new Ezy.AppAccessHandler(); |
50 | | - accessAppHandler.postHandle = function (app, data) { |
51 | | - let routerController = mvc.getController("router"); |
52 | | - routerController.updateViews('change', 'message'); |
53 | | - }; |
54 | | - |
55 | | - let exitAppHandler = new Ezy.AppExitHandler(); |
56 | | - exitAppHandler.postHandle = function (app, data) { |
57 | | - this.client.close(); |
58 | | - }; |
59 | | - |
60 | | - let connectionSuccessHandler = new Ezy.ConnectionSuccessHandler(); |
61 | | - connectionSuccessHandler.postHandle = function (event) { |
62 | | - console.log("connected to server"); |
63 | | - }; |
64 | | - |
65 | | - let disconnectionHandler = new Ezy.DisconnectionHandler(); |
66 | | - disconnectionHandler.preHandle = function (event) { |
67 | | - let routerController = mvc.getController("router"); |
68 | | - routerController.updateViews('change', 'login'); |
69 | | - }; |
70 | | - |
71 | | - let setup = client.setup; |
72 | | - setup.addEventHandler(Ezy.EventType.CONNECTION_SUCCESS, connectionSuccessHandler); |
73 | | - setup.addEventHandler(Ezy.EventType.DISCONNECTION, disconnectionHandler); |
74 | | - setup.addDataHandler(Ezy.Command.HANDSHAKE, handshakeHandler); |
75 | | - setup.addDataHandler(Ezy.Command.LOGIN, loginSuccessHandler); |
76 | | - setup.addDataHandler(Ezy.Command.LOGIN_ERROR, loginErrorHandler); |
77 | | - setup.addDataHandler(Ezy.Command.APP_ACCESS, accessAppHandler); |
78 | | - setup.addDataHandler(Ezy.Command.APP_EXIT, exitAppHandler); |
79 | | - let setupApp = setup.setupApp(APP_NAME); |
80 | | - |
81 | | - let messageController = mvc.getController("message"); |
82 | | - |
83 | | - setupApp.addDataHandler(Command.GREET, function (app, data) { |
84 | | - console.log("received message: " + JSON.stringify(data)) |
85 | | - messageController.updateViews(Command.GREET, data.message); |
86 | | - app.send(Command.SECURE_CHAT, {who: "Young Monkey"}); |
87 | | - }); |
88 | | - |
89 | | - setupApp.addDataHandler(Command.SECURE_CHAT, function (app, data) { |
90 | | - console.log("received message: " + JSON.stringify(data)) |
91 | | - messageController.updateViews(Command.SECURE_CHAT, data["secure-message"]); |
92 | | - }); |
93 | | - |
94 | | - return client; |
95 | | - } |
96 | | - |
97 | | - connect() { |
98 | | - let client = this.getClient(); |
99 | | - client.connect("ws.tvd12.com", 3005); |
100 | | - } |
101 | | - |
102 | | - isConnected() { |
103 | | - let client = this.getClient(); |
104 | | - let connected = client && client.isConnected(); |
105 | | - return connected; |
106 | | - } |
107 | | - |
108 | | - getClient() { |
109 | | - let clients = Ezy.Clients.getInstance(); |
110 | | - let client = clients.getDefaultClient(); |
111 | | - return client; |
| 8 | + static getInstance() { |
| 9 | + if (!SocketProxy.instance) { |
| 10 | + SocketProxy.instance = new SocketProxy(); |
112 | 11 | } |
| 12 | + return SocketProxy.instance; |
| 13 | + } |
| 14 | + |
| 15 | + setup() { |
| 16 | + let config = new Ezy.ClientConfig(); |
| 17 | + config.clientName = ZONE_NAME; |
| 18 | + config.enableSSL = true; |
| 19 | + config.enableDebug = true; |
| 20 | + let clients = Ezy.Clients.getInstance(); |
| 21 | + clients.newDefaultClient(config, client => { |
| 22 | + this.doSetup(client); |
| 23 | + }); |
| 24 | + } |
| 25 | + |
| 26 | + doSetup(client) { |
| 27 | + let mvc = Mvc.getInstance(); |
| 28 | + let models = mvc.models; |
| 29 | + let handshakeHandler = new Ezy.HandshakeHandler(); |
| 30 | + handshakeHandler.getLoginRequest = function () { |
| 31 | + let connection = models.connection; |
| 32 | + let username = connection.username; |
| 33 | + let password = connection.password; |
| 34 | + return [ZONE_NAME, username, password, []]; |
| 35 | + }; |
| 36 | + |
| 37 | + let loginSuccessHandler = new Ezy.LoginSuccessHandler(); |
| 38 | + loginSuccessHandler.handleLoginSuccess = function () { |
| 39 | + let accessAppRequest = [APP_NAME, []]; |
| 40 | + this.client.send(Ezy.Command.APP_ACCESS, accessAppRequest); |
| 41 | + }; |
| 42 | + |
| 43 | + let loginErrorHandler = new Ezy.LoginErrorHandler(); |
| 44 | + loginErrorHandler.handleLoginError = function (event) { |
| 45 | + let loginController = mvc.getController('login'); |
| 46 | + loginController.updateViews('loginError', event[1]); |
| 47 | + }; |
| 48 | + |
| 49 | + let accessAppHandler = new Ezy.AppAccessHandler(); |
| 50 | + accessAppHandler.postHandle = function (app, data) { |
| 51 | + let routerController = mvc.getController('router'); |
| 52 | + routerController.updateViews('change', 'message'); |
| 53 | + }; |
| 54 | + |
| 55 | + let exitAppHandler = new Ezy.AppExitHandler(); |
| 56 | + exitAppHandler.postHandle = function (app, data) { |
| 57 | + this.client.close(); |
| 58 | + }; |
| 59 | + |
| 60 | + let connectionSuccessHandler = new Ezy.ConnectionSuccessHandler(); |
| 61 | + connectionSuccessHandler.postHandle = function (event) { |
| 62 | + console.log('connected to server'); |
| 63 | + }; |
| 64 | + |
| 65 | + let disconnectionHandler = new Ezy.DisconnectionHandler(); |
| 66 | + disconnectionHandler.preHandle = function (event) { |
| 67 | + let routerController = mvc.getController('router'); |
| 68 | + routerController.updateViews('change', 'login'); |
| 69 | + }; |
| 70 | + |
| 71 | + let setup = client.setup; |
| 72 | + setup.addEventHandler( |
| 73 | + Ezy.EventType.CONNECTION_SUCCESS, |
| 74 | + connectionSuccessHandler, |
| 75 | + ); |
| 76 | + setup.addEventHandler(Ezy.EventType.DISCONNECTION, disconnectionHandler); |
| 77 | + setup.addDataHandler(Ezy.Command.HANDSHAKE, handshakeHandler); |
| 78 | + setup.addDataHandler(Ezy.Command.LOGIN, loginSuccessHandler); |
| 79 | + setup.addDataHandler(Ezy.Command.LOGIN_ERROR, loginErrorHandler); |
| 80 | + setup.addDataHandler(Ezy.Command.APP_ACCESS, accessAppHandler); |
| 81 | + setup.addDataHandler(Ezy.Command.APP_EXIT, exitAppHandler); |
| 82 | + let setupApp = setup.setupApp(APP_NAME); |
| 83 | + |
| 84 | + let messageController = mvc.getController('message'); |
| 85 | + |
| 86 | + setupApp.addDataHandler(Command.GREET, function (app, data) { |
| 87 | + console.log('received message: ' + JSON.stringify(data)); |
| 88 | + messageController.updateViews(Command.GREET, data.message); |
| 89 | + app.send(Command.SECURE_CHAT, {who: 'Young Monkey'}); |
| 90 | + }); |
| 91 | + |
| 92 | + setupApp.addDataHandler(Command.SECURE_CHAT, function (app, data) { |
| 93 | + console.log('received message: ' + JSON.stringify(data)); |
| 94 | + messageController.updateViews( |
| 95 | + Command.SECURE_CHAT, |
| 96 | + data['secure-message'], |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + return client; |
| 101 | + } |
| 102 | + |
| 103 | + connect() { |
| 104 | + let client = this.getClient(); |
| 105 | + client.connect('ws.tvd12.com', 3005); |
| 106 | + } |
| 107 | + |
| 108 | + isConnected() { |
| 109 | + let client = this.getClient(); |
| 110 | + let connected = client && client.isConnected(); |
| 111 | + return connected; |
| 112 | + } |
| 113 | + |
| 114 | + getClient() { |
| 115 | + let clients = Ezy.Clients.getInstance(); |
| 116 | + let client = clients.getDefaultClient(); |
| 117 | + return client; |
| 118 | + } |
113 | 119 | } |
114 | 120 |
|
115 | 121 | export default SocketProxy; |
0 commit comments