Last active
June 22, 2016 17:12
-
-
Save philikon/4592efd153673f3e64dec353046af7a8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class WebSocketImage extends React.Component { | |
state = { | |
blob: null, | |
}; | |
componentDidMount() { | |
let ws = this.ws = new WebSocket(...); | |
ws.binaryType = 'blob'; | |
ws.onmessage = (event) => { | |
if (this.state.blob) { | |
// Dispose of the old blob if there is one. | |
this.state.blob.close(); | |
} | |
this.setState({blob: event.data}); | |
}; | |
} | |
componentUnmount() { | |
if (this.state.blob) { | |
this.state.blob.close(); | |
} | |
this.ws.close(); | |
} | |
render() { | |
if (!this.state.blob) { | |
return <View />; | |
} | |
return <Image source={{uri: URL.createObjectURL(this.state.blob)}} />; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment