Replies: 2 comments
-
Hi there, public class GpsListenerService : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
TcpListener listener = new TcpListener(IPAddress.Any, 5001); // use a different port
listener.Start();
while (!stoppingToken.IsCancellationRequested)
{
var client = await listener.AcceptTcpClientAsync(stoppingToken);
_ = HandleClientAsync(client, stoppingToken);
}
}
private async Task HandleClientAsync(TcpClient client, CancellationToken token)
{
using var stream = client.GetStream();
using var reader = new StreamReader(stream);
while (!token.IsCancellationRequested && client.Connected)
{
var line = await reader.ReadLineAsync();
Console.WriteLine($"GPS Data: {line}");
}
}
} Remeber to register the listener: builder.Services.AddHostedService<GpsListenerService>(); |
Beta Was this translation helpful? Give feedback.
-
You can bind a SignalR endpoint to TCP, see the example: But your GPS client still needs to speak the "SignalR protocol" https://github.com/dotnet/aspnetcore/blob/main/src/SignalR/docs/specs/HubProtocol.md |
Beta Was this translation helpful? Give feedback.
-
I need help. I have a remote component that transmits data to me via TCP through a port (GPS).
in my blazor server app
Program.cs -->
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSignalR();
var app = builder.Build();
app.MapHub("/current-time");
app.Run("http://*:5000");
MainBub.cs --->
using Microsoft.AspNetCore.SignalR;
public class MainHub : Hub
{
public async IAsyncEnumerable Streaming(CancellationToken cancellationToken)
{
while (true)
{
yield return DateTime.UtcNow ;
await Task.Delay(1000, cancellationToken);
}
}
}
To know if the server was working, create a client and with it it works fine
from client side --->
using Microsoft.AspNetCore.SignalR.Client;
public class MainClient
{
public static async Task ExecuteAsync()
{
var uri = "https://localhost:5000/current-time";
await using var connection = new HubConnectionBuilder().WithUrl(uri).Build();
await connection.StartAsync();
await foreach (var date in connection.StreamAsync("Streaming"))
{
Console.WriteLine(date);
}
}
}
But the remote component (GPS) doesn't connect to the server.
Remote component data (GPS)
Domain: XXX,XX.XXX.XXX
Port: 5000
Protocol: TCP
What am I doing wrong?
Beta Was this translation helpful? Give feedback.
All reactions