How to use 'signalr'?

signalr Any examples?

I created c # server using vtortola.websockets.
I created a client in cocos creator.
It worked well on pc web.
It does not work on mobile.

So I try to use signalr.
Any examples?

Or is there no other websocket? Works on mobile.

Just simply add few lines of code at the original jquery.signalR-x.x.x.js.

Note: This is only for client side jquery signalr. Signal Official site

Steps

  1. Grab the jquery signalr and open it with your text editor tools.
  2. Copy and paste the following code at the beginning of file:

var jQuery = require("./jquery-3.3.1"); // Change the jquery version to your own version
window.jQuery = jQuery;

Paste above of this line:

(function ($, window, undefined)

  1. Copy and paste the following code at the end of file:

module.exports = window.jQuery;

  1. Remember everytime first launch of cocos creator, click “Developer” > “Compile User Scripts” (F7).
    You will get errors if you doesn’t compile your scripts.

Usage: (Sample in Typescript)

 let SignalR = require("../jquery.signalR-2.2.2");
 let connection = SignalR.hubConnection("http://localhost");
 let proxy = connection.createHubProxy('chatHub');

 // receives broadcast messages from a hub function, called "broadcastMessage"
 proxy.on('notify', this.notify.bind(this));

    // atempt connection, and handle errors
    connection.start({
        transport: [
            'webSockets', 
            //'foreverFrame', 
            //'serverSentEvents', 
            'longPolling'
        ],
        jsonp: true,
    })
    .done(function(){
        console.log('Now connected, connection ID=' + connection.id);
    })
    .fail(function(reason){
        console.log('Could not connect: ' + reason);
    });

I spent few weeks to figure out how to use signalr in cocos creator, hope this could help others.

Could you try to build ios/android? It’s working on real device or not?

So far this is only working on web platform, because this is javascript signalr with jquery.

I try to build to android, but not working.

If you want to build for android you may look for signalr java client, and ios signalr ObjC.

I have no test these 2 libraries on cocos creator yet.

I use ASP.NET Core SignalR JavaScript Client 1.0 and connect to signalR server as below:

this.connection = new signalR.HubConnectionBuilder()
            .withUrl("http://localhost:42391/signalr", {
                skipNegotiation: true,
                transport: signalR.HttpTransportType.WebSockets
            })
            .configureLogging(signalR.LogLevel.Trace)
            .build();

        this.connection.start().then(() => {
            this.lblMessage.string = 'Connected!';
        }).catch((err) => {
            this.lblMessage.string = err.toString();
        });
    
    this.connection.on('Send', (nick, message) => {
        this.lblMessage.string = `${nick}: ${message}`;
    });

It run on IOS, however it skip negotiation request. If keep negotiation request, have a error: JS: [ERROR]: Error: Failed to complete negotiation with the server: TypeError: undefined is not an object (evaluating ‘e.method’)signalR-ccc.zip (206.5 KB)

1 Like