Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error. When the server sends a message or receives a message, the connection is being disrupted. #271

Open
hasancaktar opened this issue Jun 15, 2023 · 0 comments

Comments

@hasancaktar
Copy link

hasancaktar commented Jun 15, 2023

I am trying to connect a Flutter application to a server that I developed in .NET Core. After establishing the connection, the application disconnects when it receives a message. I have been unable to find a solution. When I connect to the server using Postman, Hercules, or other client applications, there are no issues. However, I cannot establish a connection with this package.

There may be a mismatch during the handshake process. It is possible that the header is not coming correctly due to the parsing structure ("\r\n").

Errors:
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: WebSocketChannelException: WebSocketChannelException: HttpException: Invalid request method, uri = http://127.0.0.1:8080

Below is a simple example of the server application and a code snippet for a basic client in Flutter:

Server code:

using XiaoFeng.Net;

namespace AppserverTest
{
    internal class Program
    {
        static NetServer<ServerSession> server8080 = new NetServer<ServerSession>(8080);

        static void Main(string[] args)
        {
            server8080.OnStart += Server8080_OnStart;
            server8080.OnStop += Server8080_OnStop;
            server8080.OnDisconnected += Server8080_OnDisconnected;
            server8080.OnNewConnection += Server8080_OnNewConnection;
            server8080.OnMessage += Server8080_OnMessage;

            server8080.Start();
            Console.ReadLine();
        }

        private static void Server8080_OnStop(ISocket socket, EventArgs e)
        {
            Console.WriteLine("8080 stopped");
        }

        private static void Server8080_OnStart(ISocket socket, EventArgs e)
        {
            Console.WriteLine("8080 started");
        }

        private static void Server8080_OnMessage(INetSession session, string message, EventArgs e)
        {
            Console.WriteLine(message);
            session.Send("Hello");
        }

        private static void Server8080_OnNewConnection(INetSession session, EventArgs e)
        {
            Console.WriteLine("connected: " + session.Headers);
            session.Send("hello");
        }

        private static void Server8080_OnDisconnected(INetSession session, EventArgs e)
        {
            Console.WriteLine("8080 disconnected");
        }
    }
}

Client:

import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:web_socket_channel/status.dart' as status;

main() async {
  final wsUrl = Uri.parse('ws://127.0.0.1:8080')
  var channel = WebSocketChannel.connect(wsUrl);

  channel.stream.listen((message) {
    channel.sink.add('Hi!');
    //channel.sink.close(status.goingAway);
  });
}

Please note that the provided code snippets are for reference purposes, and the issue you are facing may require further analysis and debugging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant