Created
May 5, 2016 15:15
-
-
Save sethdorris/88cfa254d871a30f150537c2d66b48e4 to your computer and use it in GitHub Desktop.
Example React WebSocket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {PropTypes} from 'react'; | |
import {Link} from 'react-router'; | |
import {connect} from 'react-redux'; | |
import ws from './WebSocket'; | |
import {setUsername, socketConnectInit, socketOnOpen} from './actions/index'; | |
class Landing extends React.Component { | |
constructor(props, context) { | |
super(props, context); | |
this.connectButton = this.connectButton.bind(this); | |
} | |
async connectButton() { | |
const {dispatch} = this.props; | |
let login = document.getElementById("usernameinput").value | |
let messageobject = { | |
type: "USER_CONNECTED", | |
username: login | |
} | |
try { | |
let connected = await this.wsConnect() | |
if (connected) { | |
dispatch(socketOnOpen()); | |
dispatch(setUsername(login)); | |
await ws.sendmessage(messageobject); | |
this.context.router.push({ | |
pathname: '/main' | |
}); | |
} | |
} catch (e) { | |
console.log("Failed to connect: ", e); | |
} | |
} | |
async wsConnect() { | |
return await ws.connect(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment