Skip to content

Instantly share code, notes, and snippets.

@lazorfuzz
Last active December 13, 2018 13:27
Show Gist options
  • Select an option

  • Save lazorfuzz/d1ba91a35397e0f8e11b21adebe14d84 to your computer and use it in GitHub Desktop.

Select an option

Save lazorfuzz/d1ba91a35397e0f8e11b21adebe14d84 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import { LioWebRTC } from 'react-liowebrtc';
import './App.css';
import ChatBox from './ChatBox';
class App extends Component {
constructor(props) {
super(props);
this.state = {
chatLog: [],
options: {
debug: true,
dataOnly: true
}
};
}
join = (webrtc) => webrtc.joinRoom('my-p2p-app-demo');
handleCreatedPeer = (webrtc, peer) => {
this.addChat(`Peer-${peer.id.substring(0, 5)} joined the room!`, ' ', true);
}
handlePeerData = (webrtc, type, payload, peer) => {
switch(type) {
case 'chat':
this.addChat(`Peer-${peer.id.substring(0, 5)}`, payload);
break;
default:
return;
};
}
addChat = (name, message, alert = false) => {
this.setState({ chatLog: this.state.chatLog.concat({
name,
message: `${message}`,
timestamp: `${Date.now()}`,
alert
})});
}
render() {
const { chatLog, options } = this.state;
return (
<div className="App">
<LioWebRTC
options={options}
onReady={this.join}
onCreatedPeer={this.handleCreatedPeer}
onReceivedPeerData={this.handlePeerData}
>
<ChatBox
chatLog={chatLog}
onSend={(msg) => msg && this.addChat('Me', msg)}
/>
</LioWebRTC>
</div>
);
}
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment