mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 18:44:16 +00:00
implement LiveChat
rooms feature
This commit is contained in:
parent
0ecac67480
commit
5452b574ac
@ -1,52 +1,134 @@
|
|||||||
import React from "react"
|
import React from "react"
|
||||||
import * as antd from "antd"
|
import * as antd from "antd"
|
||||||
|
import classnames from "classnames"
|
||||||
|
import { io } from "socket.io-client"
|
||||||
|
import config from "config"
|
||||||
|
import SessionModel from "models/session"
|
||||||
|
|
||||||
import "./index.less"
|
import "./index.less"
|
||||||
|
|
||||||
const Line = (props) => {
|
const Line = (props) => {
|
||||||
const { user, content, timestamp } = props
|
const { user, content } = props
|
||||||
|
|
||||||
return <div>
|
return <div className="textRoom_line">
|
||||||
{content}
|
<div className="textRoom_line_user">
|
||||||
|
<h4>{user.fullName ?? user.username}</h4>
|
||||||
|
</div>
|
||||||
|
<div className="textRoom_line_content">
|
||||||
|
<span>{content}</span>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class LiveChat extends React.Component {
|
export default class LiveChat extends React.Component {
|
||||||
state = {
|
state = {
|
||||||
socket: null,
|
socket: null,
|
||||||
|
connecting: true,
|
||||||
|
connectionEnd: false,
|
||||||
|
roomInfo: null,
|
||||||
timeline: [],
|
timeline: [],
|
||||||
|
writtedMessage: "",
|
||||||
}
|
}
|
||||||
|
|
||||||
joinSocketRoom = () => {
|
timelineRef = React.createRef()
|
||||||
|
|
||||||
|
joinSocketRoom = async () => {
|
||||||
|
await this.setState({ connecting: true })
|
||||||
|
|
||||||
const { roomId } = this.props
|
const { roomId } = this.props
|
||||||
|
|
||||||
const socketNamespace = `/textRoom/${roomId}`
|
const socketNamespace = `/textRoom/${roomId}`
|
||||||
|
|
||||||
console.log(`Joining socket room [${socketNamespace}]`)
|
console.log(`Joining socket room [${socketNamespace}]`)
|
||||||
|
|
||||||
const socket = app.api.namespaces.main.wsInterface.manager.socket(socketNamespace)
|
const socket = io(config.remotes.messagingApi, {
|
||||||
|
transports: ["websocket"],
|
||||||
|
autoConnect: false,
|
||||||
|
})
|
||||||
|
|
||||||
socket.connect()
|
socket.auth = {
|
||||||
|
token: SessionModel.token,
|
||||||
|
}
|
||||||
|
|
||||||
console.log("Socket", socket)
|
socket.on("connect_error", (err) => {
|
||||||
|
console.error("Connection error", err)
|
||||||
|
|
||||||
|
this.setState({ connectionEnd: true })
|
||||||
|
|
||||||
|
if (err.message === "auth:token_invalid") {
|
||||||
|
console.error("Invalid token")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
socket.on("connect", () => {
|
socket.on("connect", () => {
|
||||||
console.log("Socket connected")
|
socket.emit("join", { room: socketNamespace }, (error, info) => {
|
||||||
|
if (error) {
|
||||||
|
this.setState({ connectionEnd: true })
|
||||||
|
return console.error("Error joining room", error)
|
||||||
|
}
|
||||||
|
|
||||||
socket.on("message", (data) => {
|
this.setState({
|
||||||
console.log("Message received", data)
|
connecting: false,
|
||||||
|
roomInfo: info,
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
socket.on("message", (message) => {
|
||||||
|
this.pushToTimeline(message)
|
||||||
|
this.scrollTimelineToBottom()
|
||||||
|
})
|
||||||
|
|
||||||
|
socket.connect()
|
||||||
|
|
||||||
this.setState({ socket })
|
this.setState({ socket })
|
||||||
}
|
}
|
||||||
|
|
||||||
submitMessage = (message) => {
|
submitMessage = (message) => {
|
||||||
const { socket } = this.state
|
const { socket } = this.state
|
||||||
|
|
||||||
console.log("Sending message", message)
|
socket.emit("send:message", {
|
||||||
|
message
|
||||||
|
})
|
||||||
|
|
||||||
socket.emit("message", message)
|
this.scrollTimelineToBottom()
|
||||||
|
|
||||||
|
// remove writted message
|
||||||
|
this.setState({
|
||||||
|
writtedMessage: ""
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pushToTimeline = (message) => {
|
||||||
|
const { timeline } = this.state
|
||||||
|
|
||||||
|
this.setState({
|
||||||
|
timeline: [...timeline, message]
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
handleInputChange = (e) => {
|
||||||
|
this.setState({
|
||||||
|
writtedMessage: e.target.value
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
handleOnEnter = (e) => {
|
||||||
|
e.preventDefault()
|
||||||
|
e.stopPropagation()
|
||||||
|
|
||||||
|
if (e.target.value.length === 0) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
this.submitMessage(e.target.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
scrollTimelineToBottom = () => {
|
||||||
|
// scroll to bottom smoothly
|
||||||
|
this.timelineRef.current.scrollTo({
|
||||||
|
top: this.timelineRef.current.scrollHeight,
|
||||||
|
behavior: "smooth"
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount = async () => {
|
componentDidMount = async () => {
|
||||||
@ -58,21 +140,52 @@ export default class LiveChat extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return <div className="liveChat">
|
if (this.state.connectionEnd) {
|
||||||
{this.state.timeline.map((line, index) => <Line key={index} {...line} />)}
|
return <div className="liveChat">
|
||||||
|
<antd.Result
|
||||||
|
status="error"
|
||||||
|
title="Connection error"
|
||||||
|
subTitle="Cannot connect to the server"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
|
||||||
<div className="textInput">
|
if (this.state.connecting) {
|
||||||
|
return <div className="liveChat">
|
||||||
|
<antd.Skeleton active />
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
return <div
|
||||||
|
className={classnames(
|
||||||
|
"liveChat",
|
||||||
|
{ ["empty"]: this.state.timeline.length === 0 }
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{
|
||||||
|
this.state.timeline.length === 0 ?
|
||||||
|
<antd.Empty description="Welcome to the room" /> :
|
||||||
|
<div
|
||||||
|
className="liveChat_timeline"
|
||||||
|
ref={this.timelineRef}
|
||||||
|
>
|
||||||
|
{
|
||||||
|
this.state.timeline.map((line, index) => {
|
||||||
|
return <Line key={index} {...line} />
|
||||||
|
})
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
<div className="liveChat_textInput">
|
||||||
<antd.Input.TextArea
|
<antd.Input.TextArea
|
||||||
placeholder="Type your message here"
|
placeholder="Type your message here"
|
||||||
autoSize={{ minRows: 1, maxRows: 3 }}
|
autoSize={{ minRows: 1, maxRows: 3 }}
|
||||||
onPressEnter={(e) => {
|
value={this.state.writtedMessage}
|
||||||
e.preventDefault()
|
onChange={this.handleInputChange}
|
||||||
e.stopPropagation()
|
onPressEnter={this.handleOnEnter}
|
||||||
|
maxLength={this.state.roomInfo?.limitations?.maxMessageLength ?? 100}
|
||||||
console.log("Enter pressed", e.target.value)
|
showCount
|
||||||
|
|
||||||
this.submitMessage(e.target.value)
|
|
||||||
}}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,18 +1,82 @@
|
|||||||
.liveChat {
|
.liveChat {
|
||||||
|
position: relative;
|
||||||
|
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
|
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
|
|
||||||
position: relative;
|
overflow: hidden;
|
||||||
|
|
||||||
.textInput {
|
&.empty {
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.liveChat_timeline {
|
||||||
|
height: 100%;
|
||||||
|
overflow-y: scroll;
|
||||||
|
|
||||||
|
margin-bottom: 70px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.liveChat_textInput {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
bottom: 0;
|
bottom: 0;
|
||||||
left: 0;
|
left: 0;
|
||||||
|
|
||||||
|
margin-bottom: 20px;
|
||||||
|
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: fit-content;
|
height: fit-content;
|
||||||
|
|
||||||
|
color: var(--text-color);
|
||||||
|
|
||||||
|
.ant-input-textarea-show-count::after {
|
||||||
|
color: var(--text-color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.textRoom_line {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
|
||||||
|
padding: 5px 10px;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
|
||||||
|
border-radius: 12px;
|
||||||
|
|
||||||
|
background-color: var(--background-color-accent);
|
||||||
|
|
||||||
|
h1,
|
||||||
|
h2,
|
||||||
|
h3,
|
||||||
|
h4,
|
||||||
|
span {
|
||||||
|
margin: 0;
|
||||||
|
user-select: text;
|
||||||
|
}
|
||||||
|
|
||||||
|
.textRoom_line_user {
|
||||||
|
font-weight: bold;
|
||||||
|
font-size: 1rem;
|
||||||
|
color: var(--background-color-contrast);
|
||||||
|
|
||||||
|
overflow: hidden;
|
||||||
|
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.textRoom_line_content {
|
||||||
|
font-size: 0.8rem;
|
||||||
|
color: var(--text-color);
|
||||||
|
|
||||||
|
padding: 0 10px;
|
||||||
|
|
||||||
|
word-break: break-all;
|
||||||
|
white-space: pre-wrap;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user