Skip to content

Instantly share code, notes, and snippets.

@philikon
Last active June 22, 2016 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philikon/4592efd153673f3e64dec353046af7a8 to your computer and use it in GitHub Desktop.
Save philikon/4592efd153673f3e64dec353046af7a8 to your computer and use it in GitHub Desktop.
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