Prepare calendar and ws
This commit is contained in:
		
							parent
							
								
									f43af480d0
								
							
						
					
					
						commit
						7ef9536593
					
				
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										28
									
								
								DiscoBot/ClientApp/package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										28
									
								
								DiscoBot/ClientApp/package-lock.json
									
									
									
										generated
									
									
									
								
							| @ -4211,14 +4211,12 @@ | ||||
|         "balanced-match": { | ||||
|           "version": "1.0.0", | ||||
|           "bundled": true, | ||||
|           "dev": true, | ||||
|           "optional": true | ||||
|           "dev": true | ||||
|         }, | ||||
|         "brace-expansion": { | ||||
|           "version": "1.1.11", | ||||
|           "bundled": true, | ||||
|           "dev": true, | ||||
|           "optional": true, | ||||
|           "requires": { | ||||
|             "balanced-match": "^1.0.0", | ||||
|             "concat-map": "0.0.1" | ||||
| @ -4233,20 +4231,17 @@ | ||||
|         "code-point-at": { | ||||
|           "version": "1.1.0", | ||||
|           "bundled": true, | ||||
|           "dev": true, | ||||
|           "optional": true | ||||
|           "dev": true | ||||
|         }, | ||||
|         "concat-map": { | ||||
|           "version": "0.0.1", | ||||
|           "bundled": true, | ||||
|           "dev": true, | ||||
|           "optional": true | ||||
|           "dev": true | ||||
|         }, | ||||
|         "console-control-strings": { | ||||
|           "version": "1.1.0", | ||||
|           "bundled": true, | ||||
|           "dev": true, | ||||
|           "optional": true | ||||
|           "dev": true | ||||
|         }, | ||||
|         "core-util-is": { | ||||
|           "version": "1.0.2", | ||||
| @ -4363,8 +4358,7 @@ | ||||
|         "inherits": { | ||||
|           "version": "2.0.3", | ||||
|           "bundled": true, | ||||
|           "dev": true, | ||||
|           "optional": true | ||||
|           "dev": true | ||||
|         }, | ||||
|         "ini": { | ||||
|           "version": "1.3.5", | ||||
| @ -4376,7 +4370,6 @@ | ||||
|           "version": "1.0.0", | ||||
|           "bundled": true, | ||||
|           "dev": true, | ||||
|           "optional": true, | ||||
|           "requires": { | ||||
|             "number-is-nan": "^1.0.0" | ||||
|           } | ||||
| @ -4391,7 +4384,6 @@ | ||||
|           "version": "3.0.4", | ||||
|           "bundled": true, | ||||
|           "dev": true, | ||||
|           "optional": true, | ||||
|           "requires": { | ||||
|             "brace-expansion": "^1.1.7" | ||||
|           } | ||||
| @ -4399,14 +4391,12 @@ | ||||
|         "minimist": { | ||||
|           "version": "0.0.8", | ||||
|           "bundled": true, | ||||
|           "dev": true, | ||||
|           "optional": true | ||||
|           "dev": true | ||||
|         }, | ||||
|         "minipass": { | ||||
|           "version": "2.3.5", | ||||
|           "bundled": true, | ||||
|           "dev": true, | ||||
|           "optional": true, | ||||
|           "requires": { | ||||
|             "safe-buffer": "^5.1.2", | ||||
|             "yallist": "^3.0.0" | ||||
| @ -4425,7 +4415,6 @@ | ||||
|           "version": "0.5.1", | ||||
|           "bundled": true, | ||||
|           "dev": true, | ||||
|           "optional": true, | ||||
|           "requires": { | ||||
|             "minimist": "0.0.8" | ||||
|           } | ||||
| @ -4506,8 +4495,7 @@ | ||||
|         "number-is-nan": { | ||||
|           "version": "1.0.1", | ||||
|           "bundled": true, | ||||
|           "dev": true, | ||||
|           "optional": true | ||||
|           "dev": true | ||||
|         }, | ||||
|         "object-assign": { | ||||
|           "version": "4.1.1", | ||||
| @ -4519,7 +4507,6 @@ | ||||
|           "version": "1.4.0", | ||||
|           "bundled": true, | ||||
|           "dev": true, | ||||
|           "optional": true, | ||||
|           "requires": { | ||||
|             "wrappy": "1" | ||||
|           } | ||||
| @ -4641,7 +4628,6 @@ | ||||
|           "version": "1.0.2", | ||||
|           "bundled": true, | ||||
|           "dev": true, | ||||
|           "optional": true, | ||||
|           "requires": { | ||||
|             "code-point-at": "^1.0.0", | ||||
|             "is-fullwidth-code-point": "^1.0.0", | ||||
|  | ||||
| @ -9,6 +9,7 @@ import { NavMenuComponent } from './nav-menu/nav-menu.component'; | ||||
| import { HomeComponent } from './home/home.component'; | ||||
| import { CounterComponent } from './counter/counter.component'; | ||||
| import { FetchDataComponent } from './fetch-data/fetch-data.component'; | ||||
| import { CalendarComponent } from './calendar/calendar.component'; | ||||
| 
 | ||||
| @NgModule({ | ||||
|   declarations: [ | ||||
| @ -16,7 +17,8 @@ import { FetchDataComponent } from './fetch-data/fetch-data.component'; | ||||
|     NavMenuComponent, | ||||
|     HomeComponent, | ||||
|     CounterComponent, | ||||
|     FetchDataComponent | ||||
|     FetchDataComponent, | ||||
|     CalendarComponent | ||||
|   ], | ||||
|   imports: [ | ||||
|     BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }), | ||||
| @ -26,6 +28,7 @@ import { FetchDataComponent } from './fetch-data/fetch-data.component'; | ||||
|       { path: '', component: HomeComponent, pathMatch: 'full' }, | ||||
|       { path: 'counter', component: CounterComponent }, | ||||
|       { path: 'fetch-data', component: FetchDataComponent }, | ||||
|       { path: 'calendar', component: CalendarComponent }, | ||||
|     ]) | ||||
|   ], | ||||
|   providers: [], | ||||
|  | ||||
							
								
								
									
										24
									
								
								DiscoBot/ClientApp/src/app/calendar/calendar.component.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								DiscoBot/ClientApp/src/app/calendar/calendar.component.html
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,24 @@ | ||||
| <h1>Calendar</h1> | ||||
| 
 | ||||
| <p>This component demonstrates fetching data from the server.</p> | ||||
| 
 | ||||
| <p *ngIf="!forecasts"><em>Loading...</em></p> | ||||
| 
 | ||||
| <table class='table table-striped' *ngIf="forecasts"> | ||||
|   <thead> | ||||
|     <tr> | ||||
|       <th>Date</th> | ||||
|       <th>Temp. (C)</th> | ||||
|       <th>Temp. (F)</th> | ||||
|       <th>Summary</th> | ||||
|     </tr> | ||||
|   </thead> | ||||
|   <tbody> | ||||
|     <tr *ngFor="let forecast of forecasts"> | ||||
|       <td>{{ forecast.dateFormatted }}</td> | ||||
|       <td>{{ forecast.temperatureC }}</td> | ||||
|       <td>{{ forecast.temperatureF }}</td> | ||||
|       <td>{{ forecast.summary }}</td> | ||||
|     </tr> | ||||
|   </tbody> | ||||
| </table> | ||||
							
								
								
									
										40
									
								
								DiscoBot/ClientApp/src/app/calendar/calendar.component.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								DiscoBot/ClientApp/src/app/calendar/calendar.component.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,40 @@ | ||||
| import { Component, Inject } from '@angular/core'; | ||||
| import { HttpClient } from '@angular/common/http'; | ||||
| 
 | ||||
| @Component({ | ||||
|   selector: 'app-calendar', | ||||
|   templateUrl: './calendar.component.html' | ||||
| }) | ||||
| export class CalendarComponent { | ||||
|   public forecasts: CalendarForecast[]; | ||||
| 
 | ||||
|   constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) { | ||||
|     console.log("ws connecting to ws" + baseUrl.substring(4, baseUrl.length) + "ws/calendar"); | ||||
|     var ws = new WebSocket("ws" + baseUrl.substring(4, baseUrl.length) + "ws/calendar"); | ||||
| 
 | ||||
|     ws.onopen = function () { | ||||
| 
 | ||||
|       // Web Socket is connected, send data using send()
 | ||||
|       ws.send("Message to send"); | ||||
|       alert("Message is sent..."); | ||||
|     }; | ||||
| 
 | ||||
|     ws.onmessage = function (evt) { | ||||
|       var received_msg = evt.data; | ||||
|       alert("Message is received..."); | ||||
|     }; | ||||
| 
 | ||||
|     ws.onclose = function () { | ||||
| 
 | ||||
|       // websocket is closed.
 | ||||
|       alert("Connection is closed..."); | ||||
|     }; | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| interface CalendarForecast { | ||||
|   dateFormatted: string; | ||||
|   temperatureC: number; | ||||
|   temperatureF: number; | ||||
|   summary: string; | ||||
| } | ||||
| @ -9,9 +9,9 @@ export class FetchDataComponent { | ||||
|   public forecasts: WeatherForecast[]; | ||||
| 
 | ||||
|   constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) { | ||||
|     http.get<WeatherForecast[]>(baseUrl + 'api/SampleData/WeatherForecasts').subscribe(result => { | ||||
|     /*http.get<WeatherForecast[]>(baseUrl + 'api/SampleData/WeatherForecasts').subscribe(result => { | ||||
|       this.forecasts = result; | ||||
|     }, error => console.error(error)); | ||||
|     }, error => console.error(error));*/ | ||||
|   } | ||||
| } | ||||
| 
 | ||||
|  | ||||
| @ -11,4 +11,5 @@ | ||||
|   <li><strong>Angular CLI integration</strong>. In development mode, there's no need to run <code>ng serve</code>. It runs in the background automatically, so your client-side resources are dynamically built on demand and the page refreshes when you modify any file.</li> | ||||
|   <li><strong>Efficient production builds</strong>. In production mode, development-time features are disabled, and your <code>dotnet publish</code> configuration automatically invokes <code>ng build</code> to produce minified, ahead-of-time compiled JavaScript files.</li> | ||||
| </ul> | ||||
| <app-calendar></app-calendar> | ||||
| <p>The <code>ClientApp</code> subdirectory is a standard Angular CLI application. If you open a command prompt in that directory, you can run any <code>ng</code> command (e.g., <code>ng test</code>), or use <code>npm</code> to install extra packages into it.</p> | ||||
|  | ||||
| @ -17,6 +17,9 @@ | ||||
|           <li class="nav-item" [routerLinkActive]='["link-active"]'> | ||||
|             <a class="nav-link text-dark" [routerLink]='["/fetch-data"]'>Fetch data</a> | ||||
|           </li> | ||||
|           <li class="nav-item" [routerLinkActive]='["link-active"]'> | ||||
|             <a class="nav-link text-dark" [routerLink]='["/calendar"]'>Calendar</a> | ||||
|           </li> | ||||
|         </ul> | ||||
|       </div> | ||||
|     </div> | ||||
|  | ||||
| @ -1,9 +1,12 @@ | ||||
| using Discord; | ||||
| using Discord.WebSocket; | ||||
| using Microsoft.AspNetCore.Http; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using System.Net.WebSockets; | ||||
| using System.Reflection; | ||||
| using System.Threading; | ||||
| using System.Threading.Tasks; | ||||
| 
 | ||||
| namespace DiscoBot | ||||
| @ -11,6 +14,7 @@ namespace DiscoBot | ||||
|     public class DisBot | ||||
|     { | ||||
|         private DiscordSocketClient discordClient; | ||||
|         private List<WebSocket> webSockets = new List<WebSocket>(); | ||||
|         private WSController wsC; | ||||
|         private List<Type> moduleTypes = new List<Type>(); | ||||
|         private Dictionary<ulong, Dictionary<string, Func<SocketMessage, string[], Task>>> guildcommands = new Dictionary<ulong, Dictionary<string, Func<SocketMessage, string[], Task>>>(); | ||||
| @ -19,6 +23,27 @@ namespace DiscoBot | ||||
|             this.wsC = wsC; | ||||
|         } | ||||
| 
 | ||||
|         private async Task Echo(HttpContext context, WebSocket webSocket) | ||||
|         { | ||||
|             var buffer = new byte[1024 * 4]; | ||||
|             WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); | ||||
|             while (!result.CloseStatus.HasValue) | ||||
|             { | ||||
|                 await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None); | ||||
| 
 | ||||
|                 result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); | ||||
|             } | ||||
|             //await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None); | ||||
|         } | ||||
| 
 | ||||
|         public async Task HandleWebsocket(HttpContext context) | ||||
|         { | ||||
|             Console.WriteLine("Handling Websocket to "+ context.Request.Path); | ||||
|             WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(); | ||||
|             webSockets.Add(webSocket); | ||||
|             await Echo(context, webSocket); | ||||
|         } | ||||
| 
 | ||||
|         public async void Initialize() | ||||
|         { | ||||
|             Console.WriteLine("In init!"); | ||||
|  | ||||
| @ -1,5 +1,6 @@ | ||||
| using Microsoft.AspNetCore.Builder; | ||||
| using Microsoft.AspNetCore.Hosting; | ||||
| using Microsoft.AspNetCore.Http; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
| using Microsoft.AspNetCore.SpaServices.AngularCli; | ||||
| using Microsoft.Extensions.Configuration; | ||||
| @ -8,13 +9,13 @@ using Microsoft.Extensions.Hosting; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Net.WebSockets; | ||||
| using System.Threading; | ||||
| using System.Threading.Tasks; | ||||
| 
 | ||||
| namespace DiscoBot | ||||
| { | ||||
|     public class Startup | ||||
|     { | ||||
|         private List<WebSocket> webSockets = new List<WebSocket>(); | ||||
|         private WSController wsC = new WSController(); | ||||
|         private DisBot diBot; | ||||
|         public Startup(IConfiguration configuration) | ||||
| @ -68,13 +69,11 @@ namespace DiscoBot | ||||
| 
 | ||||
|             app.Use(async (context, next) => | ||||
|             { | ||||
|                 if (context.Request.Path == "/ws") | ||||
|                 if (context.Request.Path.ToString().StartsWith("/ws")) | ||||
|                 { | ||||
|                     if (context.WebSockets.IsWebSocketRequest) | ||||
|                     { | ||||
|                         WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(); | ||||
|                         webSockets.Add(webSocket); | ||||
|                         //await Echo(context, webSocket); | ||||
|                         await diBot.HandleWebsocket(context); | ||||
|                     } | ||||
|                     else | ||||
|                     { | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user