mirror of
https://github.com/logos-messaging/logos-messaging-js.git
synced 2026-04-28 08:53:38 +00:00
Merge #58
58: Implement few commands r=D4nte a=D4nte Co-authored-by: Franck Royer <franck@status.im>
This commit is contained in:
commit
00d4c41967
@ -57,6 +57,7 @@
|
|||||||
"upgrader",
|
"upgrader",
|
||||||
"waku",
|
"waku",
|
||||||
"wakunode",
|
"wakunode",
|
||||||
|
"webfonts",
|
||||||
"websockets"
|
"websockets"
|
||||||
],
|
],
|
||||||
"flagWords": [],
|
"flagWords": [],
|
||||||
|
|||||||
@ -30,7 +30,7 @@
|
|||||||
"test": "run-s build test:*",
|
"test": "run-s build test:*",
|
||||||
"test:lint": "eslint src --ext .ts --ext .tsx",
|
"test:lint": "eslint src --ext .ts --ext .tsx",
|
||||||
"test:prettier": "prettier \"src/**/*.{ts,tsx}\" --list-different",
|
"test:prettier": "prettier \"src/**/*.{ts,tsx}\" --list-different",
|
||||||
"test:spelling": "cspell \"{README.md,.github/*.md,src/**/*.ts}\"",
|
"test:spelling": "cspell \"{README.md,.github/*.md,src/**/*.{ts,tsx},public/**/*.html}\"",
|
||||||
"fix:prettier": "prettier \"src/**/*.{ts,tsx}\" --write",
|
"fix:prettier": "prettier \"src/**/*.{ts,tsx}\" --write",
|
||||||
"fix:lint": "eslint src --ext .ts --ext .tsx --fix",
|
"fix:lint": "eslint src --ext .ts --ext .tsx --fix",
|
||||||
"js-waku:install": "cd ../; npm install",
|
"js-waku:install": "cd ../; npm install",
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"short_name": "React App",
|
"short_name": "Waku v2 chat app",
|
||||||
"name": "Create React App Sample",
|
"name": "Chat app powered by js-waku",
|
||||||
"icons": [
|
"icons": [
|
||||||
{
|
{
|
||||||
"src": "favicon.ico",
|
"src": "favicon.ico",
|
||||||
|
|||||||
@ -12,13 +12,10 @@ import { WakuContext } from './WakuContext';
|
|||||||
|
|
||||||
export const ChatContentTopic = 'dingpu';
|
export const ChatContentTopic = 'dingpu';
|
||||||
|
|
||||||
interface State {
|
|
||||||
messages: ChatMessage[];
|
|
||||||
waku?: Waku;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function App() {
|
export default function App() {
|
||||||
let [state, setState] = useState<State>({ messages: [] });
|
let [stateMessages, setMessages] = useState<ChatMessage[]>([]);
|
||||||
|
let [stateWaku, setWaku] = useState<Waku | undefined>(undefined);
|
||||||
|
let [nick, setNick] = useState<string>('web-chat');
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function initWaku() {
|
async function initWaku() {
|
||||||
@ -32,9 +29,7 @@ export default function App() {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
setState(({ messages }) => {
|
setWaku(waku);
|
||||||
return { waku, messages };
|
|
||||||
});
|
|
||||||
|
|
||||||
// FIXME: Connect to a go-waku instance by default, temporary hack until
|
// FIXME: Connect to a go-waku instance by default, temporary hack until
|
||||||
// we have a go-waku instance in the fleet
|
// we have a go-waku instance in the fleet
|
||||||
@ -53,23 +48,23 @@ export default function App() {
|
|||||||
const wakuMsg = WakuMessage.decode(event.data);
|
const wakuMsg = WakuMessage.decode(event.data);
|
||||||
if (wakuMsg.payload) {
|
if (wakuMsg.payload) {
|
||||||
const chatMsg = ChatMessage.decode(wakuMsg.payload);
|
const chatMsg = ChatMessage.decode(wakuMsg.payload);
|
||||||
const messages = state.messages.slice();
|
const messages = stateMessages.slice();
|
||||||
messages.push(chatMsg);
|
messages.push(chatMsg);
|
||||||
console.log('setState on ', messages);
|
console.log('setState on ', messages);
|
||||||
setState({ messages, waku: state.waku });
|
setMessages(messages);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!state.waku) {
|
if (!stateWaku) {
|
||||||
initWaku()
|
initWaku()
|
||||||
.then(() => console.log('Waku init done'))
|
.then(() => console.log('Waku init done'))
|
||||||
.catch((e) => console.log('Waku init failed ', e));
|
.catch((e) => console.log('Waku init failed ', e));
|
||||||
} else {
|
} else {
|
||||||
state.waku.libp2p.pubsub.on(RelayDefaultTopic, handleNewMessages);
|
stateWaku.libp2p.pubsub.on(RelayDefaultTopic, handleNewMessages);
|
||||||
|
|
||||||
// To clean up listener when component unmounts
|
// To clean up listener when component unmounts
|
||||||
return () => {
|
return () => {
|
||||||
state.waku?.libp2p.pubsub.removeListener(
|
stateWaku?.libp2p.pubsub.removeListener(
|
||||||
RelayDefaultTopic,
|
RelayDefaultTopic,
|
||||||
handleNewMessages
|
handleNewMessages
|
||||||
);
|
);
|
||||||
@ -81,17 +76,36 @@ export default function App() {
|
|||||||
let commandResponses: string[] = [];
|
let commandResponses: string[] = [];
|
||||||
const args = input.split(' ');
|
const args = input.split(' ');
|
||||||
const cmd = args.shift()!;
|
const cmd = args.shift()!;
|
||||||
const waku = state.waku;
|
if (!stateWaku) {
|
||||||
if (!waku) {
|
|
||||||
commandResponses.push('Waku is not yet initialized');
|
commandResponses.push('Waku is not yet initialized');
|
||||||
} else {
|
} else {
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case '/help':
|
case '/help':
|
||||||
|
commandResponses.push('/nick <nickname>: set a new nickname');
|
||||||
|
commandResponses.push('/info: some information about the node');
|
||||||
commandResponses.push(
|
commandResponses.push(
|
||||||
'/connect <Multiaddr>: connect to the given peer'
|
'/connect <Multiaddr>: connect to the given peer'
|
||||||
);
|
);
|
||||||
commandResponses.push('/help: Display this help');
|
commandResponses.push('/help: Display this help');
|
||||||
break;
|
break;
|
||||||
|
case '/nick':
|
||||||
|
const arg = args.shift();
|
||||||
|
if (!arg) {
|
||||||
|
commandResponses.push('No nick provided');
|
||||||
|
} else {
|
||||||
|
setNick(arg);
|
||||||
|
commandResponses.push(`New nick: ${arg}`);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case '/info':
|
||||||
|
if (!stateWaku) {
|
||||||
|
commandResponses.push(`Waku node is starting`);
|
||||||
|
} else {
|
||||||
|
commandResponses.push(
|
||||||
|
`PeerId: ${stateWaku.libp2p.peerId.toB58String()}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
case '/connect':
|
case '/connect':
|
||||||
const peer = args.shift();
|
const peer = args.shift();
|
||||||
if (!peer) {
|
if (!peer) {
|
||||||
@ -103,18 +117,18 @@ export default function App() {
|
|||||||
if (!peerId) {
|
if (!peerId) {
|
||||||
commandResponses.push('Peer Id needed to dial');
|
commandResponses.push('Peer Id needed to dial');
|
||||||
} else {
|
} else {
|
||||||
waku.libp2p.peerStore.addressBook.add(
|
stateWaku.libp2p.peerStore.addressBook.add(
|
||||||
PeerId.createFromB58String(peerId),
|
PeerId.createFromB58String(peerId),
|
||||||
[peerMultiaddr]
|
[peerMultiaddr]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
commandResponses.push('Invalid multaddr: ' + e);
|
commandResponses.push('Invalid multiaddr: ' + e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case '/peers':
|
case '/peers':
|
||||||
waku.libp2p.peerStore.peers.forEach((peer, peerId) => {
|
stateWaku.libp2p.peerStore.peers.forEach((peer, peerId) => {
|
||||||
commandResponses.push(peerId + ':');
|
commandResponses.push(peerId + ':');
|
||||||
let addresses = ' addresses: [';
|
let addresses = ' addresses: [';
|
||||||
peer.addresses.forEach(({ multiaddr }) => {
|
peer.addresses.forEach(({ multiaddr }) => {
|
||||||
@ -123,30 +137,33 @@ export default function App() {
|
|||||||
addresses = addresses.replace(/,$/, '');
|
addresses = addresses.replace(/,$/, '');
|
||||||
addresses += ']';
|
addresses += ']';
|
||||||
commandResponses.push(addresses);
|
commandResponses.push(addresses);
|
||||||
let protos = ' protos: [';
|
let protocols = ' protocols: [';
|
||||||
protos += peer.protocols;
|
protocols += peer.protocols;
|
||||||
protos += ']';
|
protocols += ']';
|
||||||
commandResponses.push(protos);
|
commandResponses.push(protocols);
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
commandResponses.push('Unknown Command');
|
commandResponses.push('Unknown Command');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setState(({ waku, messages }) => {
|
const messages = stateMessages.slice();
|
||||||
commandResponses.forEach((res) => {
|
commandResponses.forEach((res) => {
|
||||||
messages.push(new ChatMessage(new Date(), cmd, res));
|
messages.push(new ChatMessage(new Date(), cmd, res));
|
||||||
});
|
|
||||||
return { waku, messages };
|
|
||||||
});
|
});
|
||||||
|
setMessages(messages);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="App">
|
<div className="App">
|
||||||
<div className="chat-room">
|
<div className="chat-room">
|
||||||
<WakuContext.Provider value={{ waku: state.waku }}>
|
<WakuContext.Provider value={{ waku: stateWaku }}>
|
||||||
<Paper>
|
<Paper>
|
||||||
<Room lines={state.messages} commandHandler={commandHandler} />
|
<Room
|
||||||
|
nick={nick}
|
||||||
|
lines={stateMessages}
|
||||||
|
commandHandler={commandHandler}
|
||||||
|
/>
|
||||||
</Paper>
|
</Paper>
|
||||||
</WakuContext.Provider>
|
</WakuContext.Provider>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import { useWaku } from './WakuContext';
|
|||||||
interface Props {
|
interface Props {
|
||||||
lines: ChatMessage[];
|
lines: ChatMessage[];
|
||||||
commandHandler: (cmd: string) => void;
|
commandHandler: (cmd: string) => void;
|
||||||
|
nick: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Room(props: Props) {
|
export default function Room(props: Props) {
|
||||||
@ -25,7 +26,7 @@ export default function Room(props: Props) {
|
|||||||
} else {
|
} else {
|
||||||
const chatMessage = new ChatMessage(
|
const chatMessage = new ChatMessage(
|
||||||
new Date(),
|
new Date(),
|
||||||
'web-chat',
|
props.nick,
|
||||||
messageToSend
|
messageToSend
|
||||||
);
|
);
|
||||||
const wakuMsg = WakuMessage.fromBytes(
|
const wakuMsg = WakuMessage.fromBytes(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user