Guest User

Untitled

a guest
Jan 17th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. import React from 'react';
  2. import { StyleSheet, Text, View } from 'react-native';
  3. import Centrifuge from 'centrifuge';
  4.  
  5. export default class App extends React.Component {
  6.  
  7. componentDidMount() {
  8. console.log("componentDidMount")
  9. var timestamp = Math.floor(Date.now() / 1000);
  10. var centrifuge = new Centrifuge({
  11. url: 'ws://192.168.0.103:8000/connection/websocket',
  12. user: '',
  13. token: 'xyzhello',
  14. timestamp: timestamp.toString()
  15. });
  16.  
  17. var callbacks = {
  18. "message": function(message) {
  19. // See below description of message format
  20. console.log("centrifuge.message",message);
  21. },
  22. "join": function(message) {
  23. // See below description of join message format
  24. console.log("centrifuge.join",message);
  25. },
  26. "leave": function(message) {
  27. // See below description of leave message format
  28. console.log("centrifuge.leave",message);
  29. },
  30. "subscribe": function(context) {
  31. // See below description of subscribe callback context format
  32. console.log("centrifuge.subscribe",context);
  33. },
  34. "error": function(errContext) {
  35. // See below description of subscribe error callback context format
  36. console.log("centrifuge.error",err);
  37. },
  38. "unsubscribe": function(context) {
  39. // See below description of unsubscribe event callback context format
  40. console.log("centrifuge.unsubscribe",context);
  41. }
  42. }
  43.  
  44. var subscription = centrifuge.subscribe("news", callbacks);
  45. subscription.publish({"input": "hello world"}).then(function() {
  46. // success ack from Centrifugo received
  47. console.log("message sent");
  48. }, function(err) {
  49. // publish call failed with error
  50. console.log("send message failed",err);
  51. });
  52.  
  53. centrifuge.connect();
  54.  
  55. }
  56.  
  57. render() {
  58. return (
  59. <View style={styles.container}>
  60. <Text>Open up app.js to start working on your app!</Text>
  61. </View>
  62. );
  63. }
  64. }
  65.  
  66. const styles = StyleSheet.create({
  67. container: {
  68. flex: 1,
  69. backgroundColor: '#fff',
  70. alignItems: 'center',
  71. justifyContent: 'center',
  72. },
  73. });
Add Comment
Please, Sign In to add comment