diff --git a/source/chat-service/ClientApp/src/components/ChatRoom.js b/source/chat-service/ClientApp/src/components/ChatRoom.js index 3c45ada..2ee1482 100644 --- a/source/chat-service/ClientApp/src/components/ChatRoom.js +++ b/source/chat-service/ClientApp/src/components/ChatRoom.js @@ -4,13 +4,10 @@ import {HubConnectionBuilder} from '@aspnet/signalr'; export class ChatRoom extends Component { constructor(props) { super(props); - this.state = {messages: [], input: "", connected: false}; + this.state = {members: [], messages: [], input: "", connected: false}; this.alias = props.location.state.alias; - + let token = props.location.state.token; - - console.log(this.alias); - console.log(token); this.connection = new HubConnectionBuilder() .withUrl("/chat", {accessTokenFactory: () => token}) @@ -23,8 +20,10 @@ export class ChatRoom extends Component { }.bind(this)).catch(function (error) { return console.error(error.toString()); }); - + this.connection.on("ReceiveMessage", this.addMessage.bind(this)); + this.connection.on("UserConnected", this.addChatUser.bind(this)); + this.connection.on("UserDisconnected", this.removeChatUser.bind(this)); this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } @@ -35,12 +34,24 @@ export class ChatRoom extends Component { }) } + addChatUser(user) { + this.setState((state) => { + return {members: [...state.members, {user: user}]}; + }); + } + + removeChatUser(user) { + this.setState((state) => { + return {members: state.members.filter(m => m.user !== user)}; + }); + } + handleChange(event) { this.setState({input: event.target.value}) } handleSubmit(event) { - this.connection.invoke("SendMessage", this.props.alias, this.state.input).catch(err => console.error(err)); + this.connection.invoke("SendMessage", this.alias, this.state.input).catch(err => console.error(err)); event.preventDefault(); } @@ -49,11 +60,17 @@ export class ChatRoom extends Component { return (

Connecting...

); } + let users = this.state.members.map(mem =>
  • {mem.user}
  • ); + + console.dir(this.state.messages); let contents = this.state.messages.map(msg =>
  • {msg.user}: {msg.message}
  • ); return (
    + @@ -67,4 +84,8 @@ export class ChatRoom extends Component {
    ); } + + componentWillUnmount() { + this.connection.stop().catch(err => console.error(err)); + } } diff --git a/source/chat-service/ClientApp/src/components/Login.js b/source/chat-service/ClientApp/src/components/Login.js index 9f89919..6b3cb37 100644 --- a/source/chat-service/ClientApp/src/components/Login.js +++ b/source/chat-service/ClientApp/src/components/Login.js @@ -21,7 +21,7 @@ export class Login extends Component { 'Password': this.state.password }) }).then(response => response.json()).then(response => { - this.setState({token: response.Token, alias: response.Alias, authorized: true}) + this.setState({token: response.token, alias: response.alias, authorized: true}) }); event.preventDefault(); diff --git a/source/chat-service/Hubs/ChatHub.cs b/source/chat-service/Hubs/ChatHub.cs index a983dde..cab17dc 100644 --- a/source/chat-service/Hubs/ChatHub.cs +++ b/source/chat-service/Hubs/ChatHub.cs @@ -1,3 +1,4 @@ +using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.SignalR; @@ -11,5 +12,17 @@ public async Task SendMessage(string user, string message) { await Clients.All.SendAsync("ReceiveMessage", user, message); } + + public override async Task OnConnectedAsync() + { + await Clients.All.SendAsync("UserConnected", Context.User.Identity.Name); + await base.OnConnectedAsync(); + } + + public override async Task OnDisconnectedAsync(Exception exception) + { + await Clients.All.SendAsync("UserDisconnected", Context.User.Identity.Name); + await base.OnDisconnectedAsync(exception); + } } } diff --git a/source/chat-service/Services/AuthService.cs b/source/chat-service/Services/AuthService.cs index 7a4777c..80f2bcd 100644 --- a/source/chat-service/Services/AuthService.cs +++ b/source/chat-service/Services/AuthService.cs @@ -28,8 +28,9 @@ public AuthService(ChatDbContext chatDbContext, IOptions secur public async Task<(ChatUser User, string Token)> Authenticate(string userName, string password) { - var userAuth = await ChatDbContext.Auths.FirstOrDefaultAsync(auth => auth.Login == userName - && auth.Password == password); + var userAuth = await ChatDbContext.Auths.Include(auth => auth.User) + .FirstOrDefaultAsync(auth => auth.Login == userName + && auth.Password == password); if (userAuth == null) {