olofd / react-native-signalr

Use SignalR with React Native
150 stars 61 forks source link

Unable to call signalR method #57

Closed hardikcc closed 4 years ago

hardikcc commented 4 years ago

I am trying to connect signalR from react-native app. If I open the connection, before registering proxy, connection happens. When I register proxy before the connection is opened, it fails the negotiation. If I register the proxy.on after connection. function is not being called. Please advise.

`import React, {Component} from 'react'; import {Text, StyleSheet, View} from 'react-native';

import signalr from 'react-native-signalr';

export default class SignalTest extends Component { constructor(props) { super(props); } sendDriverUpdate = async => { this.connection .invoke('driverLocationUpdate', '2', '1.1221', '3.12312') .catch(err => console.error(err)); }; componentDidMount() { const connection = signalr.hubConnection('http://164.68.125.218', { headers: { token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6ImIiLCJuYmYiOjE1ODI4NzM2NDMsImV4cCI6MTU4NTQ2NTY0MywiaWF0IjoxNTgyODczNjQzLCJhdWQiOiIxNjQuNjguMTI1LjIxOCJ9.uRh1ExYW0y3RwYCOP6IetD_MEgkmyviwvW-_4iCQ_1k', username: 'b', driverid: 2, }, }); connection.logging = true;

const proxy = connection.createHubProxy('AgentHub');

proxy.on(
  'driverLocationUpdate',
  (2, '1.1222', '3.23232'),
  msgfromserver => {
    console.log(msgfromserver, argOne, argTwo, argThree);
    //Here I could response by calling something else on the server...
  })

connection
  .start(() => {})
  .done(() => {

  })
  .fail(  (err, err1) => {
    console.log('Failed');
    console.log(err);
  });
/* connection.disconnected(function() {
  setTimeout(function() {
    connection.start().done(function() {
      console.log('reconnected');
    });
  }, 3000);
}); */

}

render() { return (

textInComponent
);

} }

const styles = StyleSheet.create({}); `