programmerJavaPL
programmerJavaPL

Reputation: 496

Connection of websocket with an android application

I have an error when trying to connect:

ReactNativeJS: [12:52:08 GMT+0000 (GMT)] SignalR: Client subscribed to hub 'chathub'. ReactNativeJS: [12:52:08 GMT+0000 (GMT)] SignalR: Negotiating with 'http://10.0.2.2:50069/chathub/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D'. ReactNativeJS: 'SignalR error: Error during negotiation request.', '' ReactNativeJS: Failed ReactNativeJS: [12:52:08 GMT+0000 (GMT)] SignalR: Stopping connection.

Server:

public class ChatHub : Hub
{
    public async Task SendMessage(string user, string message)
    {
        await Clients.All.SendAsync("ReceiveMessage", user, message);
    }
}

In Startup.cs (Unbound entries are omitted)

public void ConfigureServices(IServiceCollection services)
{

    services.AddSignalR();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, DataContext context)
{
    // global cors policy
    app.UseCors(x => x
        .AllowAnyOrigin()
        .AllowAnyMethod()
        .AllowAnyHeader()
        .AllowCredentials());

    app.UseMvc();

    app.UseSignalR(routes =>
    {
        routes.MapHub<ChatHub>("/chatHub");
    });
}

React-native:

import signalr from 'react-native-signalr';

export default class Settings extends Component{

  componentDidMount() {
    //This is the server under /example/server published on azure.
    const connection = signalr.hubConnection('http://10.0.2.2:50069/chathub');
    connection.logging = true;

    const proxy = connection.createHubProxy('chatHub');
    //receives broadcast messages from a hub function, called "helloApp"
    proxy.on('SendMessage', (argOne, argTwo, argThree, argFour) => {
      console.log('message-from-server', argOne, argTwo, argThree, argFour);
      //Here I could response by calling something else on the server...
    });

    // atempt connection, and handle errors
    connection.start().done(() => {
      console.log('Now connected, connection ID=' + connection.id);

      proxy.invoke('sendMessage', 'Hello Server, how are you?', 'ffff')
        .done((directResponse) => {
          console.log('direct-response-from-server', directResponse);
        }).fail(() => {
          console.warn('Something went wrong when calling server, it might not be up and running?')
        });

    }).fail(() => {
      console.log('Failed');
    });

    //connection-handling
    connection.connectionSlow(() => {
      console.log('We are currently experiencing difficulties with the connection.')
    });

    connection.error((error) => {
      const errorMessage = error.message;
      let detailedError = '';
      if (error.source && error.source._response) {
        detailedError = error.source._response;
      }
      if (detailedError === 'An SSL error has occurred and a secure connection to the server cannot be made.') {
        console.log('When using react-native-signalr on ios with http remember to enable http in App Transport Security https://github.com/olofd/react-native-signalr/issues/14')
      }
      console.debug('SignalR error: ' + errorMessage, detailedError)
    });
  }
    render(){
        return(
          <View style={{flex: 1}}>
        </View>
        )
    }
}

I do everything as it should be but I do not want to make a connection. I am using Windows 10.

Maybe I forget something in the code?

................................................................................................................................................................................................................................................

Upvotes: 2

Views: 991

Answers (1)

Mikael Mengistu
Mikael Mengistu

Reputation: 342

It looks like you're trying to use an ASP.NET Core SignalR server with an ASP.NET SignalR Client. Core and non Core versions of the client and server are incompatible.

From the MSDN SignalR alpha blog post

SignalR for ASP.NET Core is not compatible with previous versions of SignalR. This means that you cannot use the old server with the new clients or the old clients with the new server.

Upvotes: 2

Related Questions