Skip to content

Commit 7b77376

Browse files
committed
websocket chat service added (simple python api server for test)
1 parent 8b9a993 commit 7b77376

11 files changed

+131
-24
lines changed

src/app/app.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { UserInfoComponent } from './main-page/user-info/user-info.component';
88
import { StrangerInfoComponent } from './main-page/stranger-info/stranger-info.component';
99
import { ChatComponent } from './chat/chat.component';
1010
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
11+
import { ChatService } from "./chat.service"
1112

1213
@NgModule({
1314
declarations: [
@@ -23,7 +24,7 @@ import { PageNotFoundComponent } from './page-not-found/page-not-found.component
2324
AppRoutingModule,
2425
FormsModule
2526
],
26-
providers: [],
27+
providers: [ChatService],
2728
bootstrap: [AppComponent]
2829
})
2930
export class AppModule { }

src/app/chat.service.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { TestBed } from '@angular/core/testing';
2+
3+
import { ChatService } from './chat.service';
4+
5+
describe('ChatService', () => {
6+
let service: ChatService;
7+
8+
beforeEach(() => {
9+
TestBed.configureTestingModule({});
10+
service = TestBed.inject(ChatService);
11+
});
12+
13+
it('should be created', () => {
14+
expect(service).toBeTruthy();
15+
});
16+
});

src/app/chat.service.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Injectable } from '@angular/core';
2+
import { webSocket, WebSocketSubject } from 'rxjs/webSocket';
3+
import { ServerConfig } from './server-config';
4+
import { catchError, tap, switchAll } from 'rxjs/operators';
5+
import { EMPTY, Subject } from 'rxjs';
6+
7+
@Injectable({
8+
providedIn: 'root'
9+
})
10+
export class Format {
11+
constructor(
12+
public message: string
13+
){}
14+
}
15+
export class ChatService {
16+
17+
private socket$: WebSocketSubject<Format>;
18+
private messagesSubject$ = new Subject();
19+
20+
public connect(): void {
21+
22+
if (!this.socket$ || this.socket$.closed) {
23+
this.socket$ = this.getNewWebSocket();
24+
}
25+
}
26+
27+
getObservable(){
28+
return this.socket$.asObservable();
29+
}
30+
31+
private getNewWebSocket() {
32+
console.log(ServerConfig.getUrl())
33+
return webSocket<Format>(ServerConfig.getUrl());
34+
}
35+
36+
sendMessage(msg: string) {
37+
this.socket$.next( new Format(msg) );
38+
}
39+
40+
close() {
41+
this.socket$.complete(); }
42+
}

src/app/chat/chat.component.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@
3030
}
3131
.msg {
3232
float: left;
33+
word-break: break-all;
34+
}
35+
.robotMsg{
36+
border-bottom: 1px solid black;
3337
}
3438
.messagePrefix{
3539
float: left;
3640
font-weight: bold;
3741
}
3842
.myTextInput{
3943
color: white;
44+
border: 1px solid black !important;
4045
}

src/app/chat/chat.component.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
<section class="chat dispHeight">
22
<div class="row TextBox m-0">
33
<div class="h-100 col-12 px-0 px-md-5 pt-1 pt-md-2 pb-0">
4+
<div #RobotMessages class="backgroundGrey d-block w-100 robotMsg px-4 py-2">{{robotMessage + ' '}} </div>
45
<div #msgContainer class="backgroundGrey chatContent d-block h-100 w-100 px-1">
56
<div class="msgContainer" *ngFor="let message of chatMessages; trackBy: identify">
6-
<div [class]="message.getUserString() + ' pl-1 d-block messagePrefix'">{{message.getMessagePrefix() + ': '}}</div>
7-
<div class="msg d-block pl-1">{{message.message}}</div>
7+
<div [class]="message.getUserString() + ' px-1 d-block messagePrefix'">{{message.getMessagePrefix() + ': '}}</div>
8+
<div class="msg d-block pl-1" *ngFor="let msgRow of message.message; first as f" [class.clearfix]="!f">
9+
{{msgRow}} <br>
10+
</div>
811
<div class="clearfix"></div>
912
</div>
1013
</div>
@@ -23,7 +26,7 @@
2326
</div>
2427
<div class="h-100 col-10 px-0 py-0">
2528
<div class="backgroundGrey d-block h-100 w-100">
26-
<input #textInput type="text" class="backgroundGrey myTextInput w-100 h-100 px-2 py-1" ngModel (ngModelChange)="onTextChange($event)" (keyup.enter)="sendMessage(textInput.value)">
29+
<textarea #textInput type="text" class="backgroundGrey myTextInput w-100 h-100 px-2 py-1" ngModel (ngModelChange)="onTextChange($event)" (keyup.enter)="sendMessage(textInput.value)"></textarea>
2730
</div>
2831
</div>
2932
<div class="h-100 col-1 px-0 pr-md-5 py-0 text-center">

src/app/chat/chat.component.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,62 @@
11
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
2+
import { ChatService } from '../chat.service'
23
import { NgModule } from '@angular/core';
34
import { Message, User } from "./message";
5+
import { catchError, map, tap } from 'rxjs/operators';
46

57
@Component({
68
selector: 'app-chat',
79
templateUrl: './chat.component.html',
810
styleUrls: ['./chat.component.css']
911
})
10-
export class ChatComponent implements OnInit {
1112

13+
export class ChatComponent implements OnInit {
14+
RobotMessages = {
15+
connecting: "Connecting to server"
16+
}
17+
liveData$
1218
chatMessages: Message[] = [];
1319
counter: number = 0;
14-
20+
robotMessage: string = ""
1521
@ViewChild('textInput') inputField: ElementRef;
1622
@ViewChild('msgContainer') msgContainer: ElementRef;
1723

18-
constructor() { }
19-
24+
constructor(private chatService: ChatService) {
25+
}
26+
2027
ngOnInit(): void {
28+
this.robotMessage = this.RobotMessages.connecting;
29+
this.chatService.connect();
2130

31+
this.chatService.getObservable().subscribe(
32+
msg => this.postMessage(User.stranger, msg.message),
33+
// Called whenever there is a message from the server
34+
err => console.log(err),
35+
// Called if WebSocket API signals some kind of error
36+
() => console.log('complete')
37+
// Called when connection is closed (for whatever reason)
38+
);
39+
console.log("init ended")
2240
}
2341

2442
onTextChange(event){
2543
console.log(this.counter++);
2644
}
2745

28-
sendMessage(message: string){
29-
if(message.length == 0)
30-
return;
46+
postMessage(who: User, message: string){
47+
if(! (message.length == 0 || message.length == 1)) {
48+
let msg: Message = new Message(who, message.slice(0,-1).split('\n')); //remove enter and split
49+
this.chatMessages.push(msg);
50+
}
51+
}
3152

32-
let msg: Message = new Message(User.you, message);
33-
this.chatMessages.push(msg);
34-
console.log(msg);
53+
sendMessage(message: string){
54+
console.log(message)
55+
this.postMessage(User.you, message);
3556
this.inputField.nativeElement.value = '';
3657
let msgCont = this.msgContainer.nativeElement;
3758
msgCont.scrollTop = msgCont.scrollHeight;
59+
this.chatService.sendMessage(message);
3860
}
3961

4062
identify(index, message: Message){

src/app/chat/message.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export class Message {
55
public id: number;
66
constructor(
77
public user: User,
8-
public message: string
8+
public message: string[]
99
){
1010
this.id = Message.idCounter++;
1111
}

src/app/main-page/stranger-info/stranger-info.component.html

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
<div class="row justify-content-start">
22
<div class="col-12 col-md-4 pl-5">
33
<fieldset class="form-group">
4-
<legend class="w-100 ">Twoja Płeć</legend>
4+
<legend class="w-100 ">Płeć rozmówcy</legend>
55
<div class="form-check">
6-
<input class="form-check-input" type="radio" name="gridRadios" id="gridRadios1" value="option1" checked>
7-
<label class="form-check-label" for="gridRadios1">
6+
<input class="form-check-input" type="radio" name="gridRadios" id="gridRadios11" value="option1" checked>
7+
<label class="form-check-label" for="gridRadios11">
88
Mężczyzna
99
</label>
1010
</div>
1111
<div class="form-check">
12-
<input class="form-check-input" type="radio" name="gridRadios" id="gridRadios2" value="option2">
13-
<label class="form-check-label" for="gridRadios2">
12+
<input class="form-check-input" type="radio" name="gridRadios" id="gridRadios21" value="option2">
13+
<label class="form-check-label" for="gridRadios21">
1414
Kobieta
1515
</label>
1616
</div>
1717
<div class="form-check">
18-
<input class="form-check-input" type="radio" name="gridRadios" id="gridRadios3" value="option3">
19-
<label class="form-check-label" for="gridRadios3">
18+
<input class="form-check-input" type="radio" name="gridRadios" id="gridRadios31" value="option3">
19+
<label class="form-check-label" for="gridRadios31">
2020
Inna
2121
</label>
2222
</div>
2323
<div class="form-check">
24-
<input class="form-check-input" type="radio" name="gridRadios" id="gridRadios4" value="option4">
25-
<label class="form-check-label" for="gridRadios4">
24+
<input class="form-check-input" type="radio" name="gridRadios" id="gridRadios41" value="option4">
25+
<label class="form-check-label" for="gridRadios41">
2626
Nie podano
2727
</label>
2828
</div>

src/app/server-config.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { ServerConfig } from './server-config';
2+
3+
describe('ServerConfig', () => {
4+
it('should create an instance', () => {
5+
expect(new ServerConfig()).toBeTruthy();
6+
});
7+
});

src/app/server-config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export class ServerConfig {
2+
public static ip: string = "localhost"
3+
public static port: number = 8765;
4+
public static protocol: string = "ws"
5+
public static getUrl(){
6+
return this.protocol + "://" + this.ip + ':' + this.port;
7+
}
8+
}

src/styles.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ body {
6868
button, input {
6969
border-width: 0;
7070
}
71+
.clearfix{
72+
clear: both;
73+
}
7174
.nofocus:focus {
7275
outline: none;
7376
}

0 commit comments

Comments
 (0)