Browse Source

Prepare calendar and ws

master
amki 6 years ago
parent
commit
7ef9536593
  1. BIN
      DiscoBot/98293701175955456-rss.db
  2. 28
      DiscoBot/ClientApp/package-lock.json
  3. 5
      DiscoBot/ClientApp/src/app/app.module.ts
  4. 24
      DiscoBot/ClientApp/src/app/calendar/calendar.component.html
  5. 40
      DiscoBot/ClientApp/src/app/calendar/calendar.component.ts
  6. 4
      DiscoBot/ClientApp/src/app/fetch-data/fetch-data.component.ts
  7. 1
      DiscoBot/ClientApp/src/app/home/home.component.html
  8. 3
      DiscoBot/ClientApp/src/app/nav-menu/nav-menu.component.html
  9. 25
      DiscoBot/DisBot.cs
  10. 9
      DiscoBot/Startup.cs

BIN
DiscoBot/98293701175955456-rss.db

Binary file not shown.

28
DiscoBot/ClientApp/package-lock.json

@ -4211,14 +4211,12 @@
"balanced-match": { "balanced-match": {
"version": "1.0.0", "version": "1.0.0",
"bundled": true, "bundled": true,
"dev": true, "dev": true
"optional": true
}, },
"brace-expansion": { "brace-expansion": {
"version": "1.1.11", "version": "1.1.11",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"balanced-match": "^1.0.0", "balanced-match": "^1.0.0",
"concat-map": "0.0.1" "concat-map": "0.0.1"
@ -4233,20 +4231,17 @@
"code-point-at": { "code-point-at": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true, "bundled": true,
"dev": true, "dev": true
"optional": true
}, },
"concat-map": { "concat-map": {
"version": "0.0.1", "version": "0.0.1",
"bundled": true, "bundled": true,
"dev": true, "dev": true
"optional": true
}, },
"console-control-strings": { "console-control-strings": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true, "bundled": true,
"dev": true, "dev": true
"optional": true
}, },
"core-util-is": { "core-util-is": {
"version": "1.0.2", "version": "1.0.2",
@ -4363,8 +4358,7 @@
"inherits": { "inherits": {
"version": "2.0.3", "version": "2.0.3",
"bundled": true, "bundled": true,
"dev": true, "dev": true
"optional": true
}, },
"ini": { "ini": {
"version": "1.3.5", "version": "1.3.5",
@ -4376,7 +4370,6 @@
"version": "1.0.0", "version": "1.0.0",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"number-is-nan": "^1.0.0" "number-is-nan": "^1.0.0"
} }
@ -4391,7 +4384,6 @@
"version": "3.0.4", "version": "3.0.4",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"brace-expansion": "^1.1.7" "brace-expansion": "^1.1.7"
} }
@ -4399,14 +4391,12 @@
"minimist": { "minimist": {
"version": "0.0.8", "version": "0.0.8",
"bundled": true, "bundled": true,
"dev": true, "dev": true
"optional": true
}, },
"minipass": { "minipass": {
"version": "2.3.5", "version": "2.3.5",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"safe-buffer": "^5.1.2", "safe-buffer": "^5.1.2",
"yallist": "^3.0.0" "yallist": "^3.0.0"
@ -4425,7 +4415,6 @@
"version": "0.5.1", "version": "0.5.1",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"minimist": "0.0.8" "minimist": "0.0.8"
} }
@ -4506,8 +4495,7 @@
"number-is-nan": { "number-is-nan": {
"version": "1.0.1", "version": "1.0.1",
"bundled": true, "bundled": true,
"dev": true, "dev": true
"optional": true
}, },
"object-assign": { "object-assign": {
"version": "4.1.1", "version": "4.1.1",
@ -4519,7 +4507,6 @@
"version": "1.4.0", "version": "1.4.0",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"wrappy": "1" "wrappy": "1"
} }
@ -4641,7 +4628,6 @@
"version": "1.0.2", "version": "1.0.2",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"code-point-at": "^1.0.0", "code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0", "is-fullwidth-code-point": "^1.0.0",

5
DiscoBot/ClientApp/src/app/app.module.ts

@ -9,6 +9,7 @@ import { NavMenuComponent } from './nav-menu/nav-menu.component';
import { HomeComponent } from './home/home.component'; import { HomeComponent } from './home/home.component';
import { CounterComponent } from './counter/counter.component'; import { CounterComponent } from './counter/counter.component';
import { FetchDataComponent } from './fetch-data/fetch-data.component'; import { FetchDataComponent } from './fetch-data/fetch-data.component';
import { CalendarComponent } from './calendar/calendar.component';
@NgModule({ @NgModule({
declarations: [ declarations: [
@ -16,7 +17,8 @@ import { FetchDataComponent } from './fetch-data/fetch-data.component';
NavMenuComponent, NavMenuComponent,
HomeComponent, HomeComponent,
CounterComponent, CounterComponent,
FetchDataComponent FetchDataComponent,
CalendarComponent
], ],
imports: [ imports: [
BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }), BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
@ -26,6 +28,7 @@ import { FetchDataComponent } from './fetch-data/fetch-data.component';
{ path: '', component: HomeComponent, pathMatch: 'full' }, { path: '', component: HomeComponent, pathMatch: 'full' },
{ path: 'counter', component: CounterComponent }, { path: 'counter', component: CounterComponent },
{ path: 'fetch-data', component: FetchDataComponent }, { path: 'fetch-data', component: FetchDataComponent },
{ path: 'calendar', component: CalendarComponent },
]) ])
], ],
providers: [], providers: [],

24
DiscoBot/ClientApp/src/app/calendar/calendar.component.html

@ -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

@ -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;
}

4
DiscoBot/ClientApp/src/app/fetch-data/fetch-data.component.ts

@ -9,9 +9,9 @@ export class FetchDataComponent {
public forecasts: WeatherForecast[]; public forecasts: WeatherForecast[];
constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) { 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; this.forecasts = result;
}, error => console.error(error)); }, error => console.error(error));*/
} }
} }

1
DiscoBot/ClientApp/src/app/home/home.component.html

@ -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>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> <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> </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> <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>

3
DiscoBot/ClientApp/src/app/nav-menu/nav-menu.component.html

@ -17,6 +17,9 @@
<li class="nav-item" [routerLinkActive]='["link-active"]'> <li class="nav-item" [routerLinkActive]='["link-active"]'>
<a class="nav-link text-dark" [routerLink]='["/fetch-data"]'>Fetch data</a> <a class="nav-link text-dark" [routerLink]='["/fetch-data"]'>Fetch data</a>
</li> </li>
<li class="nav-item" [routerLinkActive]='["link-active"]'>
<a class="nav-link text-dark" [routerLink]='["/calendar"]'>Calendar</a>
</li>
</ul> </ul>
</div> </div>
</div> </div>

25
DiscoBot/DisBot.cs

@ -1,9 +1,12 @@
using Discord; using Discord;
using Discord.WebSocket; using Discord.WebSocket;
using Microsoft.AspNetCore.Http;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.WebSockets;
using System.Reflection; using System.Reflection;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace DiscoBot namespace DiscoBot
@ -11,6 +14,7 @@ namespace DiscoBot
public class DisBot public class DisBot
{ {
private DiscordSocketClient discordClient; private DiscordSocketClient discordClient;
private List<WebSocket> webSockets = new List<WebSocket>();
private WSController wsC; private WSController wsC;
private List<Type> moduleTypes = new List<Type>(); 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>>>(); 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; 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() public async void Initialize()
{ {
Console.WriteLine("In init!"); Console.WriteLine("In init!");

9
DiscoBot/Startup.cs

@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SpaServices.AngularCli; using Microsoft.AspNetCore.SpaServices.AngularCli;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
@ -8,13 +9,13 @@ using Microsoft.Extensions.Hosting;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.WebSockets; using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace DiscoBot namespace DiscoBot
{ {
public class Startup public class Startup
{ {
private List<WebSocket> webSockets = new List<WebSocket>();
private WSController wsC = new WSController(); private WSController wsC = new WSController();
private DisBot diBot; private DisBot diBot;
public Startup(IConfiguration configuration) public Startup(IConfiguration configuration)
@ -68,13 +69,11 @@ namespace DiscoBot
app.Use(async (context, next) => app.Use(async (context, next) =>
{ {
if (context.Request.Path == "/ws") if (context.Request.Path.ToString().StartsWith("/ws"))
{ {
if (context.WebSockets.IsWebSocketRequest) if (context.WebSockets.IsWebSocketRequest)
{ {
WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(); await diBot.HandleWebsocket(context);
webSockets.Add(webSocket);
//await Echo(context, webSocket);
} }
else else
{ {

Loading…
Cancel
Save