Skip to content

Commit b83f4b4

Browse files
authored
Merge pull request #1 from gouravdwivedi6590/code-separation
Code separation
2 parents ef4c23d + bc545d7 commit b83f4b4

File tree

8 files changed

+372
-175
lines changed

8 files changed

+372
-175
lines changed

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Microsoft.AspNetCore.Authentication.JwtBearer;
2-
using Microsoft.AspNetCore.Authorization;
1+
using Microsoft.AspNetCore.Authorization;
32
using Microsoft.AspNetCore.SignalR;
43
using System;
54
using System.Threading.Tasks;
@@ -16,7 +15,13 @@ public override Task OnConnectedAsync()
1615
public async Task TestCall(string data)
1716
{
1817
var connectionId = Context.ConnectionId;
19-
await Clients.Client(connectionId).SendAsync("ReceiveData", $"Data Received from TestCall method: {data}");
18+
await Clients.Client(connectionId).SendAsync("ReceiveData", $"Data Received from TestCall method: {data}");
19+
}
20+
21+
public async Task TestCall1(string data, string d)
22+
{
23+
var connectionId = Context.ConnectionId;
24+
await Clients.Client(connectionId).SendAsync("ReceiveData", $"Data Received from TestCall method: {data}- {d}");
2025
}
2126

2227
//[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
@@ -32,15 +37,28 @@ public async Task NotifyAllClient(string data)
3237
await Clients.All.SendAsync("ReceiveData", $"Data Received: {data}");
3338
}
3439

35-
public async Task TestComplexData(string data)
40+
public async Task TestComplexData(User data)
3641
{
3742
var connectionId = Context.ConnectionId;
38-
await Clients.Client(connectionId).SendAsync("ReceiveData", $"Data Received: {data}");
43+
await Clients.Client(connectionId).SendAsync("ComplexSample", new { Msg = "Data Received", User = data });
3944
}
4045

4146
public override Task OnDisconnectedAsync(Exception exception)
4247
{
4348
return base.OnDisconnectedAsync(exception);
4449
}
4550
}
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+
}
4664
}

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>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import * as appSignalR from './lib/app.signalr';
2+
3+
var sr = null;
4+
class AppLogic {
5+
6+
constructor() {
7+
this.isTokenRequired = false;
8+
this.isBasicView = true;
9+
}
10+
11+
Init(options) {
12+
13+
if(this.isBasicView !== true && this.isTokenRequired === true) {
14+
//adding new property to options object
15+
options["isTokenRequired"] = true;
16+
}
17+
else {
18+
options["isTokenRequired"] = false;
19+
}
20+
21+
sr = new appSignalR.SignalRApp(options.url);
22+
sr.Init(options);
23+
}
24+
25+
OnConnect() {
26+
sr.OnConnect();
27+
}
28+
29+
OnSend(options) {
30+
sr.OnSend(options);
31+
}
32+
33+
OnReceive(callback) {
34+
sr.OnReceive(callback);
35+
}
36+
37+
OnDisConnect() {
38+
sr.OnDisConnect();
39+
}
40+
41+
EnableAuth() {
42+
this.isTokenRequired = true;
43+
}
44+
45+
DisableAuth() {
46+
this.isTokenRequired = false;
47+
}
48+
49+
IsAuthEnabled() {
50+
return this.isTokenRequired;
51+
}
52+
53+
GetCurrentView() {
54+
return this.isBasicView;
55+
}
56+
57+
SetCurrentViewAsBasic() {
58+
this.isBasicView = true;
59+
}
60+
61+
SetCurrentViewAsAdvance() {
62+
this.isBasicView = false;
63+
}
64+
}
65+
66+
export { AppLogic }
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import mitt from 'mitt';
2+
3+
const ContentType = {
4+
TEXT : "Text",
5+
NUMBER : "Number",
6+
JSON : "JSON"
7+
}
8+
9+
const AppEvents = mitt();
10+
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: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import * as SignalR from "@aspnet/signalr";
2+
import { AppEvents } from './app.common';
3+
4+
class SignalRApp {
5+
constructor(url) {
6+
this.url = url;
7+
this.connection = null;
8+
this.isConnected = false;
9+
this.processResponse = null;
10+
}
11+
12+
Init(options) {
13+
14+
if(options.isTokenRequired === true) {
15+
options.accessTokenFactory = () => options.getToken();
16+
}
17+
18+
this.connection = new SignalR.HubConnectionBuilder()
19+
.withUrl(options.url, options)
20+
.configureLogging(SignalR.LogLevel.Information)
21+
.build();
22+
23+
//Receive Data
24+
//Reading the raw response
25+
var self = this;
26+
self.processResponse = self.connection.processIncomingData;
27+
28+
self.connection.processIncomingData = function (data) {
29+
self.processResponse.call(self.connection, data);
30+
self.HandleResponse(data);
31+
}
32+
33+
AppEvents.emit('Init', options);
34+
AppEvents.emit('Logger', "Init");
35+
}
36+
37+
38+
OnConnect() {
39+
var self = this;
40+
self.connection.start()
41+
.then(function () {
42+
AppEvents.emit('OnConnect', { url: self.url });
43+
})
44+
.catch(function (err) {
45+
AppEvents.emit('Logger', err.toString());
46+
return console.error(err.toString());
47+
});
48+
}
49+
50+
OnSend(options) {
51+
var methodArguments = new Array();
52+
methodArguments = options.methodArguments;
53+
54+
AppEvents.emit('OnSend', options);
55+
AppEvents.emit('Logger', "Calling... " + options.methodName);
56+
this.connection.invoke(options.methodName, ...methodArguments)
57+
.catch(function (err) {
58+
AppEvents.emit('Logger', err.toString());
59+
return console.log(err);
60+
});
61+
}
62+
63+
OnReceive(callback) {
64+
// this.connection.on("ReceiveData", function (data) {
65+
// callback(data);
66+
// });
67+
}
68+
69+
OnDisConnect() {
70+
this.connection.stop()
71+
.then(function () {
72+
console.log('Disconnected');
73+
AppEvents.emit('Logger', "Disconnected...");
74+
AppEvents.emit('OnDisconnected');
75+
})
76+
.catch(function (err) {
77+
AppEvents.emit('Logger', err.toString());
78+
return console.error(err.toString());
79+
});
80+
}
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+
}
117+
}
118+
119+
export { SignalRApp }

src/js/components/srform.component.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
1-
import * as abcTest from './srform';
1+
import * as srFrom from './srform';
22

33
class SrFormComponent extends HTMLElement {
44
constructor() {
55
super();
66
}
77

88
static get observedAttributes() {
9-
return ['type'];
9+
return ['type'];
1010
}
1111
get getType() {
12-
return this.getAttribute('type');
12+
return this.getAttribute('type');
1313
}
1414

1515
connectedCallback() {
16-
console.log('connected!');
1716
this.render();
18-
abcTest.Init();
19-
17+
srFrom.Init();
2018
}
2119

2220
disconnectedCallback() {
23-
console.log('disconnected!');
2421
}
2522

2623
attributeChangedCallback(attrName, oldVal, newVal) {
@@ -33,6 +30,13 @@ class SrFormComponent extends HTMLElement {
3330
this.innerHTML = `
3431
<form>
3532
<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+
3640
<div class="form-group row">
3741
<label for="inputUrl" class="col-sm-2 col-form-label">Service Endpoint</label>
3842
<div class="col-sm-10">
@@ -108,10 +112,19 @@ class SrFormComponent extends HTMLElement {
108112
</div>
109113
<div class="form-group row onconnect ">
110114
<div class="col-sm-2 offset-sm-2 btn-group">
111-
<input type="button" class="btn btn-primary btn-send-payload" value="Send"/>
115+
<input type="button" class="btn btn-primary btn-send-payload" id="btn-send-payload" value="Send"/>
112116
<input type="button" class="btn btn-primary disconnectbtn" id="btn-disconnectbtn" value="Disconnect" />
113117
</div>
114118
</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>
115128
</div>
116129
</fieldset>
117130
</form>

0 commit comments

Comments
 (0)