2021-04-19 14:54:39 +10:00
|
|
|
import { Paper } from '@material-ui/core';
|
2021-04-21 15:51:52 +10:00
|
|
|
import { multiaddr } from 'multiaddr';
|
|
|
|
|
import PeerId from 'peer-id';
|
2021-04-14 14:44:12 +10:00
|
|
|
import React from 'react';
|
|
|
|
|
import './App.css';
|
2021-04-21 15:51:52 +10:00
|
|
|
import { ChatMessage } from 'waku-chat/chat_message';
|
|
|
|
|
import { WakuMessage } from 'waku/waku_message';
|
|
|
|
|
import { RelayDefaultTopic } from 'waku/waku_relay';
|
2021-04-16 11:32:00 +10:00
|
|
|
import Room from './Room';
|
2021-04-21 15:51:52 +10:00
|
|
|
import Waku from 'waku/waku';
|
2021-04-19 11:34:13 +10:00
|
|
|
import { WakuContext } from './WakuContext';
|
2021-04-14 14:44:12 +10:00
|
|
|
|
2021-04-21 15:51:52 +10:00
|
|
|
export const ChatContentTopic = 'dingpu';
|
|
|
|
|
|
2021-04-14 15:23:00 +10:00
|
|
|
interface Props {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface State {
|
2021-04-19 11:34:13 +10:00
|
|
|
messages: string[],
|
2021-04-21 15:51:52 +10:00
|
|
|
waku?: Waku
|
2021-04-14 15:23:00 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class App extends React.Component<Props, State> {
|
2021-04-21 15:51:52 +10:00
|
|
|
waku?: Waku;
|
2021-04-19 11:34:13 +10:00
|
|
|
|
2021-04-14 15:23:00 +10:00
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
2021-04-19 10:36:37 +10:00
|
|
|
messages: []
|
2021-04-14 15:23:00 +10:00
|
|
|
};
|
2021-04-19 10:36:37 +10:00
|
|
|
|
2021-04-21 15:51:52 +10:00
|
|
|
Waku.create({}).then((waku) => {
|
|
|
|
|
this.state = { waku: waku, messages: this.state.messages };
|
|
|
|
|
|
|
|
|
|
waku.libp2p.peerStore.addressBook
|
|
|
|
|
.add(PeerId.createFromB58String('QmUJKveCpfwA4cY1zRybMEt5z64FRtMHLFFQwndWrSfMmf'),
|
|
|
|
|
[multiaddr('/ip4/127.0.0.1/tcp/7777/ws')]);
|
|
|
|
|
|
|
|
|
|
waku.libp2p.pubsub.on(RelayDefaultTopic, (event) => {
|
|
|
|
|
const wakuMsg = WakuMessage.decode(event.data);
|
|
|
|
|
if (wakuMsg.payload) {
|
|
|
|
|
const chatMsg = ChatMessage.decode(wakuMsg.payload);
|
|
|
|
|
const msgStr = printMessage(chatMsg);
|
2021-04-19 11:34:13 +10:00
|
|
|
|
2021-04-21 15:51:52 +10:00
|
|
|
const messages = this.state.messages.slice();
|
|
|
|
|
messages.push(msgStr);
|
|
|
|
|
this.setState({ messages });
|
|
|
|
|
}
|
2021-04-19 11:34:13 +10:00
|
|
|
|
|
|
|
|
});
|
2021-04-21 15:51:52 +10:00
|
|
|
|
|
|
|
|
// Fire and forget
|
|
|
|
|
// waku.dial('/ip4/127.0.0.1/tcp/7777/ws/p2p/QmUJKveCpfwA4cY1zRybMEt5z64FRtMHLFFQwndWrSfMmf').then(() => {
|
|
|
|
|
// console.log('Remote node dialed');
|
|
|
|
|
// }).catch((e) => {
|
|
|
|
|
// console.log('Error when dialing peer ', e);
|
|
|
|
|
// });
|
|
|
|
|
}).catch((e) => {
|
|
|
|
|
console.log('Error starting waku ', e);
|
|
|
|
|
}).then(()=> {
|
|
|
|
|
console.log("Waku is started");
|
2021-04-19 11:34:13 +10:00
|
|
|
});
|
2021-04-14 15:23:00 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
return (
|
|
|
|
|
<div className='App'>
|
2021-04-16 11:32:00 +10:00
|
|
|
<div className='chat-room'>
|
2021-04-19 11:34:13 +10:00
|
|
|
<WakuContext.Provider value={{ waku: this.state.waku }}>
|
2021-04-21 15:51:52 +10:00
|
|
|
<Paper>
|
2021-04-19 14:54:39 +10:00
|
|
|
<Room lines={this.state.messages} />
|
|
|
|
|
</Paper>
|
2021-04-19 11:34:13 +10:00
|
|
|
</WakuContext.Provider>
|
2021-04-14 15:23:00 +10:00
|
|
|
</div>
|
2021-04-14 15:13:55 +10:00
|
|
|
</div>
|
2021-04-14 15:23:00 +10:00
|
|
|
);
|
|
|
|
|
}
|
2021-04-14 14:44:12 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default App;
|
2021-04-21 15:51:52 +10:00
|
|
|
|
|
|
|
|
// TODO: Make it a proper component
|
|
|
|
|
function printMessage(chatMsg: ChatMessage) {
|
|
|
|
|
const timestamp = chatMsg.timestamp.toLocaleString([], {
|
|
|
|
|
month: 'short',
|
|
|
|
|
day: 'numeric',
|
|
|
|
|
hour: 'numeric',
|
|
|
|
|
minute: '2-digit',
|
|
|
|
|
hour12: false
|
|
|
|
|
});
|
|
|
|
|
return `<${timestamp}> ${chatMsg.nick}: ${chatMsg.message}`;
|
|
|
|
|
}
|