Record Ocelot + SignalR multi server test

Keywords: ASP.NET JSON less

Preface

There are two projects, one gateway and one SignalR

Paste code

1,Gatway

1. Reference to Ocelot

2. Add a little code

Startup.cs

3. Simple configuration of ocelot

ocelot.json

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/{catchAll}", //Downstream path
      "DownstreamScheme": "ws", //https / / downstream protocol
      "DownstreamHostAndPorts": [ // Downstream host and port
        {
          "Host": "127.0.0.1",  // Here is the signal address after me
          "Port": 53353
        },
        {
          "Host": "127.0.0.1",
          "Port": 53354
        },
        {
          "Host": "127.0.0.1",
          "Port": 53355
        }
      ],
      "UpstreamPathTemplate": "/gateway/{catchAll}", // Upstream path
      "UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE", "OPTIONS" ], //http method used upstream
      "LoadBalancerOptions": {
        "Type": "RoundRobin" //Rain and dew are evenly distributed.
        //Less tasks to receive customers
        //NoLoadBalance will be released to Sri Lanka
      }
    }
  ],
  "GlobalConfiguration": {  //Global configuration
    "BaseUrl": "http://127.0.0.1:5000"
  }
}

2,signalr

1,Startup.cs

2,chat.js

//const connection = new signalR.HubConnectionBuilder()
//    . withUrl("http://127.0.0.1:5000/gateway/chatHub") / / use HTTP here
//    .configureLogging(signalR.LogLevel.Information)
//    .build();

const connection = new signalR.HubConnectionBuilder()
    .withUrl("ws://127.0.0.1:5000/gateway/chatHub ", {/ / WebSockets are used here. Otherwise, it cannot be connected
        skipNegotiation: true,
        transport: signalR.HttpTransportType.WebSockets
    })
    .configureLogging(signalR.LogLevel.Trace)
    .build();

connection.on("ReceiveMessage", (user, message) => {
    const encodedMsg = user + " says " + message;
    const li = document.createElement("li");
    li.textContent = encodedMsg;
    document.getElementById("messagesList").appendChild(li);
});

document.getElementById("sendButton").addEventListener("click", event => {
    const user = document.getElementById("userInput").value;
    const message = document.getElementById("messageInput").value;
    connection.invoke("SendMessage", user, message).catch(err => console.error(err.toString()));
    event.preventDefault();
});

connection.start().catch(err => console.error(err.toString()));

3,Program.cs

test

1. Start three signalrs

2. Start Gateway project

3. Start the client

Three new clients were opened and found to be assigned to three addresses.

That is to say, the three servers should not be able to send messages. Let's test it here.

Try two more clients

I accidentally sent a message of 54. Let's see if there is any news in the previous 54.

Yes, indeed.

4. End of test

OK, the test is over. I didn't see the Ocelot source code.

The conclusion is that Ocelot plays with SignalR. I can't play happily together.

So use other ways to do it.

Posted by Draicone on Thu, 21 Nov 2019 06:43:25 -0800