Skip to content

Commit bc545d7

Browse files
Gourav DwivediGourav Dwivedi
authored andcommitted
Added logger functionality
1 parent b79ce07 commit bc545d7

File tree

7 files changed

+194
-103
lines changed

7 files changed

+194
-103
lines changed

Samples/AspDotCore/WebApp/WebApp/Hubs/OneHub.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,28 @@ public async Task NotifyAllClient(string data)
3737
await Clients.All.SendAsync("ReceiveData", $"Data Received: {data}");
3838
}
3939

40-
public async Task TestComplexData(string data)
40+
public async Task TestComplexData(User data)
4141
{
4242
var connectionId = Context.ConnectionId;
43-
await Clients.Client(connectionId).SendAsync("ReceiveData", $"Data Received: {data}");
43+
await Clients.Client(connectionId).SendAsync("ComplexSample", new { Msg = "Data Received", User = data });
4444
}
4545

4646
public override Task OnDisconnectedAsync(Exception exception)
4747
{
4848
return base.OnDisconnectedAsync(exception);
4949
}
5050
}
51+
52+
public class User
53+
{
54+
public int Id { get; set; }
55+
public string Name { get; set; }
56+
public Address Address { get; set; }
57+
}
58+
59+
public class Address
60+
{
61+
public string Country { get; set; }
62+
public int Pin { get; set; }
63+
}
5164
}

src/css/main.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,15 @@ ul.nav .nav-link.active:hover {
9999
border-color: white !important;
100100
}
101101

102+
.logger-container {
103+
padding: 5px;
104+
}
105+
106+
.bg-gray {
107+
background-color:#cccccc;
108+
border-color: black;
109+
border-width: 2px;
110+
border-style: double;
111+
padding: 10px;
112+
}
113+

src/js/components/app.component.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ class AppComponent extends HTMLElement {
3434
<div id="basicview" class="tab-pane active">
3535
<h2>Basic</h2>
3636
<sr-form></sr-form>
37-
</div>
38-
39-
37+
</div>
4038
</div>
4139
</div>
4240
</div>

src/js/components/logic/lib/app.common.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,43 @@ const ContentType = {
88

99
const AppEvents = mitt();
1010

11-
export { ContentType, AppEvents };
11+
function DisableElementByClassName(className) {
12+
var el = document.getElementsByClassName(className);
13+
14+
for (var i = 0; i < el.length; i++) {
15+
el[i].disabled = true;
16+
}
17+
}
18+
19+
function EnableElementByClassName(className) {
20+
var el = document.getElementsByClassName(className);
21+
22+
for (var i = 0; i < el.length; i++) {
23+
el[i].disabled = false;
24+
}
25+
}
26+
27+
function HideElementByClassName(className) {
28+
var el = document.getElementsByClassName(className);
29+
30+
for (var i = 0; i < el.length; i++) {
31+
el[i].style.display = "node";
32+
}
33+
}
34+
35+
function ShowElementByClassName(className) {
36+
var el = document.getElementsByClassName(className);
37+
38+
for (var i = 0; i < el.length; i++) {
39+
el[i].style.display = "block";
40+
}
41+
}
42+
43+
export {
44+
ContentType,
45+
AppEvents,
46+
EnableElementByClassName,
47+
DisableElementByClassName,
48+
HideElementByClassName,
49+
ShowElementByClassName
50+
};
Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import * as signalR from "@aspnet/signalr";
1+
import * as SignalR from "@aspnet/signalr";
22
import { AppEvents } from './app.common';
33

4-
var storeFunc = null;
54
class SignalRApp {
65
constructor(url) {
76
this.url = url;
87
this.connection = null;
9-
this.isConnected = false;
8+
this.isConnected = false;
9+
this.processResponse = null;
1010
}
1111

1212
Init(options) {
@@ -15,22 +15,23 @@ class SignalRApp {
1515
options.accessTokenFactory = () => options.getToken();
1616
}
1717

18-
this.connection = new signalR.HubConnectionBuilder()
18+
this.connection = new SignalR.HubConnectionBuilder()
1919
.withUrl(options.url, options)
20-
.configureLogging(signalR.LogLevel.Information)
20+
.configureLogging(SignalR.LogLevel.Information)
2121
.build();
22-
2322

2423
//Receive Data
2524
//Reading the raw response
2625
var self = this;
27-
storeFunc = self.connection.processIncomingData;
26+
self.processResponse = self.connection.processIncomingData;
27+
2828
self.connection.processIncomingData = function (data) {
29-
console.log('processIncomingData'+ data);
30-
storeFunc.call(self.connection, data);
29+
self.processResponse.call(self.connection, data);
30+
self.HandleResponse(data);
3131
}
32-
debugger;
32+
3333
AppEvents.emit('Init', options);
34+
AppEvents.emit('Logger', "Init");
3435
}
3536

3637

@@ -41,6 +42,7 @@ class SignalRApp {
4142
AppEvents.emit('OnConnect', { url: self.url });
4243
})
4344
.catch(function (err) {
45+
AppEvents.emit('Logger', err.toString());
4446
return console.error(err.toString());
4547
});
4648
}
@@ -50,28 +52,68 @@ class SignalRApp {
5052
methodArguments = options.methodArguments;
5153

5254
AppEvents.emit('OnSend', options);
55+
AppEvents.emit('Logger', "Calling... " + options.methodName);
5356
this.connection.invoke(options.methodName, ...methodArguments)
5457
.catch(function (err) {
58+
AppEvents.emit('Logger', err.toString());
5559
return console.log(err);
5660
});
5761
}
5862

5963
OnReceive(callback) {
60-
this.connection.on("ReceiveData", function (data) {
61-
callback(data);
62-
});
64+
// this.connection.on("ReceiveData", function (data) {
65+
// callback(data);
66+
// });
6367
}
6468

6569
OnDisConnect() {
6670
this.connection.stop()
6771
.then(function () {
6872
console.log('Disconnected');
73+
AppEvents.emit('Logger', "Disconnected...");
6974
AppEvents.emit('OnDisconnected');
7075
})
7176
.catch(function (err) {
77+
AppEvents.emit('Logger', err.toString());
7278
return console.error(err.toString());
7379
});
7480
}
81+
82+
HandleResponse(input) {
83+
AppEvents.emit('Logger', input.toString());
84+
var output = this.ParseRespose(input);
85+
if(output !== null) {
86+
87+
output.forEach((e) => {
88+
var jsonObj = JSON.parse(e);
89+
90+
if(jsonObj !== null && jsonObj.hasOwnProperty('target')) {
91+
debugger;
92+
AppEvents.emit('ReceivedData', { "ClientMethod": jsonObj.target, "Data": jsonObj.arguments });
93+
}
94+
});
95+
}
96+
}
97+
98+
ParseRespose(input) {
99+
100+
if (typeof input !== "string") {
101+
console.log("Invalid input for JSON hub protocol. Expected a string.");
102+
return null;
103+
//throw new Error("Invalid input for JSON hub protocol. Expected a string.");
104+
}
105+
106+
var separator = String.fromCharCode(0x1e);
107+
if (input[input.length - 1] !== separator) {
108+
console.log("Message is incomplete.");
109+
//throw new Error("Message is incomplete.");
110+
return null;
111+
}
112+
113+
var messages = input.split(separator);
114+
messages.pop();
115+
return messages;
116+
}
75117
}
76118

77119
export { SignalRApp }

src/js/components/srform.component.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ class SrFormComponent extends HTMLElement {
3030
this.innerHTML = `
3131
<form>
3232
<fieldset>
33+
<div class="form">
34+
<div class="checkbox-container float-right">
35+
<input type="checkbox" id="chk-loggerView" value="Logger View" />
36+
<label for="chk-loggerView" class="col-sm-2 col-form-label">Logging</label>
37+
</div>
38+
</div>
39+
3340
<div class="form-group row">
3441
<label for="inputUrl" class="col-sm-2 col-form-label">Service Endpoint</label>
3542
<div class="col-sm-10">
@@ -109,6 +116,15 @@ class SrFormComponent extends HTMLElement {
109116
<input type="button" class="btn btn-primary disconnectbtn" id="btn-disconnectbtn" value="Disconnect" />
110117
</div>
111118
</div>
119+
<div class="form-group row logger-container" style="display:none" id="logger-container">
120+
<fieldset class="bg-gray" id="loggerView">
121+
<legend class="col-form-label">
122+
<h3>Logs</h3>
123+
</legend>
124+
<div class="container" id="app-logs">
125+
126+
<div>
127+
</fieldset>
112128
</div>
113129
</fieldset>
114130
</form>

0 commit comments

Comments
 (0)