Last active
February 27, 2020 13:55
-
-
Save sembug/682256765bdeae8e82e4093df2dfb413 to your computer and use it in GitHub Desktop.
Revisions
-
sembug revised this gist
Feb 26, 2020 . 2 changed files with 52 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ import { Component, OnInit } from '@angular/core'; import { NamesService } from './services/names.service'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit { title = 'ngWebSocket'; list: string[] = []; constructor(private service: NamesService) { } ngOnInit() { this.service.commands$.subscribe(c => this.handleCommand(c)); } ngOnDestroy() { this.service.commands$.unsubscribe(); } private add(list: string[], name: string) { return list.concat(name); } private handleCommand(name: string) { console.log(name); this.list = this.add(this.list, name); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ import { OnDestroy, Injectable } from '@angular/core'; import { webSocket, WebSocketSubject } from "rxjs/webSocket"; @Injectable({ providedIn: 'root', }) export class NamesService implements OnDestroy { private _commands$: WebSocketSubject<any>; constructor() { this._commands$ = webSocket('ws://localhost:8088'); } ngOnDestroy() { this._commands$.unsubscribe(); } get commands$() { return this._commands$; } } -
sembug created this gist
Feb 26, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,39 @@ using SuperWebSocket; using System; using System.Threading; namespace ConsoleWebSocket { public class Program { private static WebSocketServer webSocketServer; public static void Main(string[] args) { webSocketServer = new WebSocketServer(); int port = 8088; webSocketServer.Setup(port); webSocketServer.NewSessionConnected += WebSocketServer_NewSessionConnected; webSocketServer.SessionClosed += WebSocketServer_SessionClosed; webSocketServer.Start(); Console.WriteLine($"Server started @{port}."); Console.ReadKey(); Console.WriteLine($"Server stopped."); } private static void WebSocketServer_SessionClosed(WebSocketSession session, SuperSocket.SocketBase.CloseReason value) { Console.WriteLine($"Session closed."); } private static void WebSocketServer_NewSessionConnected(WebSocketSession session) { Console.WriteLine($"Session started: {session.CurrentToken}."); while(session.Connected) { session.Send("{\"time\" : \"" + DateTime.Now.ToLongTimeString() + "\"}"); Thread.Sleep(5000); } } } }