|
24 | 24 |
|
25 | 25 | <br /> |
26 | 26 | <div id="connectionContainer"> |
| 27 | + <div class="row" data-bind="visible: typeof(connectionId()) != 'undefined'"> |
| 28 | + <div class="col-sm-12 col-md-12 col-lg-12"> |
| 29 | + <div class="alert alert-success"> |
| 30 | + ConnectionId: <span data-bind="text: connectionId"></span> |
| 31 | + </div> |
| 32 | + </div> |
| 33 | + </div> |
27 | 34 | <div class="row"> |
28 | 35 | <div class="col-sm-12 col-md-12 col-lg-12"> |
29 | 36 | <div class="alert alert-warning" data-bind="html: logInfo"> |
|
36 | 43 | <div class="input-group"> |
37 | 44 | <input type="text" class="form-control" disabled="disabled" data-bind="value: connectionUrl"> |
38 | 45 | <div class="input-group-btn"> |
39 | | - <button type="button" data-bind="click: connect, text: connectionState, css: connectionStatus"> |
40 | | - </button> |
| 46 | + <button type="button" data-bind="click: connect, text: connectionState, css: connectionStatus"></button> |
41 | 47 | </div> |
42 | 48 | </div> |
43 | 49 | <br /> |
|
80 | 86 | $(function () { |
81 | 87 | 'use strict'; |
82 | 88 |
|
| 89 | + var connectorName = "@Constants.ConnectorName" |
83 | 90 | var containerId = "connectionContainer"; |
84 | 91 | var scheme = document.location.protocol == "https:" ? "wss" : "ws"; |
85 | 92 | var port = document.location.port ? (":" + document.location.port) : ""; |
86 | 93 | var socket; |
87 | 94 |
|
| 95 | + var WebSocketCommands = { |
| 96 | + Connect: 1, |
| 97 | + DataSend: 2, |
| 98 | + Handshake: 4, |
| 99 | + All: 7 |
| 100 | + } |
| 101 | +
|
88 | 102 | var state = { |
89 | 103 | connect: "Connect", |
90 | 104 | connecting: "Connecting...", |
|
93 | 107 |
|
94 | 108 | var ViewModel = function () { |
95 | 109 | var that = this; |
| 110 | + this.connectionId = ko.observable(); |
96 | 111 | this.data = ko.observable(); |
97 | 112 | this.dataRows = ko.observableArray([]); |
98 | | - this.connectionUrl = ko.observable(scheme + "://" + document.location.hostname + port); |
| 113 | + this.connectionUrl = ko.observable(scheme + "://" + document.location.hostname + port + "?connectorName=" + connectorName); |
99 | 114 | this.logInfo = ko.observable("Waiting for connection. To connect click the Connect button"); |
100 | 115 | this.connectionState = ko.observable(state.connect); |
101 | 116 | this.connectionStatus = ko.pureComputed(function () { |
|
139 | 154 | } |
140 | 155 | }; |
141 | 156 | socket.onmessage = function (event) { |
142 | | - var row = { |
143 | | - dataFrom: "Server", |
144 | | - dataTo: "Client", |
145 | | - colorFrom: "row-server", |
146 | | - colorTo: "row-client", |
147 | | - context: event.data |
148 | | - }; |
149 | | - vm.dataRows.push(row); |
| 157 | +
|
| 158 | + if (event.data) { |
| 159 | +
|
| 160 | + var context = JSON.parse(event.data); |
| 161 | +
|
| 162 | + console.log(vm.connectionId()); |
| 163 | + if (context && context.Command == WebSocketCommands.Handshake) { |
| 164 | + vm.connectionId(context.Value); |
| 165 | + } |
| 166 | +
|
| 167 | + var row = { |
| 168 | + dataFrom: "Server", |
| 169 | + dataTo: "Client", |
| 170 | + colorFrom: "row-server", |
| 171 | + colorTo: "row-client", |
| 172 | + context: event.data |
| 173 | + }; |
| 174 | + vm.dataRows.push(row); |
| 175 | + } |
150 | 176 | }; |
151 | 177 | }; |
152 | 178 |
|
|
162 | 188 | if (!socket || socket.readyState != WebSocket.OPEN) { |
163 | 189 | vm.logInfo("Not connected!"); |
164 | 190 | } |
165 | | - var data = vm.data(); |
| 191 | + var data = JSON.stringify({ header: { "connectorName": connectorName, "connectionId": vm.connectionId(), }, value: vm.data() }); |
166 | 192 | socket.send(data); |
167 | 193 | var row = { |
168 | 194 | dataFrom: "Client", |
|
0 commit comments