所以我正在尝试使用 chatkit,他们有我们使用的连接功能。
但是我正在尝试获取 currentuser 对象,因此我可以在其他功能中使用它。但是,对象始终未定义,函数也未定义。
我正在使用这个 sdk https://docs.pusher.com/chatkit/reference/javascript
我尝试了const user和this.user。
import React, { Component } from 'react';
import ToggleButton from './ToggleButton';
import ChatBox from './ChatBox';
import { ChatManager, TokenProvider } from '@pusher/chatkit'
class App extends Component {
constructor(props) {
super(props);
this.state = {
chatbox: true,
user: ''
}
const chatManager = new ChatManager({
instanceLocator: 'somestring',
userId: 'JaneLowTeu',
tokenProvider: new TokenProvider({ url: 'https://us1.pusherplatform.io/services/chatkit_token_provider/v1/somestring/token' })
})
this.user = chatManager.connect() // <-- I want to save the object. Tried both this.user and const user =
.then(currentUser => {
console.log('Successful connection', currentUser)
return currentUser;
})
.catch(err => {
console.log('Error on connection', err)
})
}
joinRoom = () => {
this.user.createRoom({ //<- When I click the button i want to createRoom but now it says user is undefined, function is undefined.
name: 'general',
private: true,
addUserIds: ['craig', 'kate']
}).then(room => {
console.log(`Created room called ${room.name}`)
})
.catch(err => {
console.log(`Error creating room ${err}`)
})
}
showChatBox = () => {
console.log(this.state.chatbox);
if (this.state.chatbox) {
return (<ChatBox />);
}
}
toggleChatBox = () => {
this.setState(prevState => ({ chatbox: !prevState.chatbox }))
}
render() {
return (
<div>
<div
onClick={() => this.joinRoom()}
>Join Room</div>
<ToggleButton onClick={this.toggleChatBox}/>
{this.showChatBox()}
</div>
)
}
}
export default App;