-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
flask-sock websocket chat implementation
- Loading branch information
Showing
6 changed files
with
76 additions
and
136 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,33 @@ | ||
import {Injectable} from "@angular/core"; | ||
// import type {WebSocketSubject} from "rxjs/webSocket"; | ||
// import {webSocket} from "rxjs/webSocket"; | ||
import type {Socket} from "socket.io-client"; | ||
import {io} from "socket.io-client"; | ||
import type {WebSocketSubject} from "rxjs/webSocket"; | ||
import {webSocket} from "rxjs/webSocket"; | ||
|
||
interface IMessage { | ||
type: string; | ||
data: string; | ||
} | ||
type IMessage = string; | ||
|
||
@Injectable({ | ||
providedIn: "root", | ||
}) | ||
export class WebSocketService { | ||
// private socket: WebSocketSubject<string>; | ||
private socket: Socket; | ||
private socket: WebSocketSubject<IMessage>; | ||
messages$: IMessage[] = []; | ||
|
||
constructor() { | ||
// this.socket = webSocket("ws://localhost/"); | ||
this.socket = io("http://localhost", { | ||
transports: ["websocket"], | ||
}); | ||
this.socket.on("message", (data: IMessage) => { | ||
console.log(data); | ||
this.messages$.push(data); | ||
this.socket = webSocket("http://localhost/ws"); | ||
this.socket.subscribe({ | ||
next: (msg) => this.messages$.push(msg), // Called whenever there is a message from the server. | ||
error: (err) => console.log(err), // Called if at any point WebSocket API signals some kind of error. | ||
complete: () => this.socket.next("disconnect"), // Called when connection is closed (for whatever reason). | ||
}); | ||
} | ||
|
||
// Send a message to the server | ||
sendMessage(message: IMessage) { | ||
console.log("Send happened: ", message); | ||
this.socket.send(message); | ||
// this.messages$.push(message); | ||
// this.socket.next(JSON.stringify(message)); | ||
this.socket.next(message); | ||
} | ||
|
||
listenSocket() {} | ||
|
||
// Receive messages from the server | ||
/* listenSocket(): void { | ||
console.log("Listening..."); | ||
this.socket.subscribe({ | ||
next: (msg) => this.messages$.push(msg), // Called whenever there is a message from the server. | ||
error: (err) => console.log(err), // Called if at any point WebSocket API signals some kind of error. | ||
complete: () => console.log("complete"), // Called when connection is closed (for whatever reason). | ||
}); | ||
console.log("Messages: ", this.messages$); | ||
}*/ | ||
|
||
// Close the WebSocket connection | ||
closeConnection() { | ||
this.socket.off(); | ||
// this.socket.complete(); | ||
this.socket.complete(); | ||
} | ||
} |
This file contains 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 characters
Oops, something went wrong.