Coder Social home page Coder Social logo

Comments (18)

konsultaner avatar konsultaner commented on June 19, 2024 1

@jiajun-ifanr I have tried some code where you can so something like this

var options = new SubscribeOptions();
options.addCustomValue("where", (serializerType) => "{}");
final subscription = await session.subscribe(topic, options: options);

This makes it possible to pass a custom field with a custom serializer for either msgpack or json to a subscribe option. I tried it with your code an the server responded with a subscribed as expected. I will publish the code soon. I just need to write some tests and add the custom options to all the other message types as well.

from connectanum-dart.

konsultaner avatar konsultaner commented on June 19, 2024 1

@jiajun-ifanr The issue is that you printing args instead of argKwds.

print(event.argumentsKeywords);
// or 
print((event.argumentsKeywords['after'] as Map)['text']);

this works

from connectanum-dart.

konsultaner avatar konsultaner commented on June 19, 2024 1

@jiajun-ifanr I'm very eger to make this module work as good as possible. I have not used it in production but I will soon. So having people to test it for me is just perfekt. So you helped me too. 😅

from connectanum-dart.

konsultaner avatar konsultaner commented on June 19, 2024

The SubscribeOptions are used to support WAMP advanced fatures like Pattern Based Subscription . This is where you need the match option.

Since dart does not support a reflection api, I hard coded all SubscribeOptions that I knew exists.

What are the information you want to share during the subscription process? What is the router you use? What advanced feature do you want to use that is not supported by this package?

from connectanum-dart.

jiajun-ifanr avatar jiajun-ifanr commented on June 19, 2024

It would probably be better if I explain it this way:

I have an issue when subscribe to a topic.

I used the PubSub pattern and followed the example given in the main.dart.

It was originally thought to send a Map as an option to the server on subscribe, and this Map could contain a where Map, like this:

{"where": {}}

Since the where key is a custom key required by our server, it will look like this:

[
        32,
        912873614,
        {
            "where": {}
        },
        "com.myapp.topic.emergency"
]

However, even when I execute the below code without an option, there are two errors shown:

  1. Unhandled Exception: Bad state: No element
  2. Unhandled Exception: NoSuchMethodError: The getter 'inMilliseconds' was called on null.

Say for example I have the following code in my main.dart (without a SubscribeOptions passed):

import 'package:flutter/material.dart';
import 'package:connectanum/connectanum.dart';
import 'package:connectanum/json.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Home(),
    );
  }
}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  void initState() {
    super.initState();
    test();
  }

  void test() async {
    WampImplementation wamp = new WampImplementation();
    wamp.subscribe();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Test App'),
      ),
    );
  }
}

class WampImplementation {
  Client client;

  void subscribe() async {
    String url =
        'wss://a4d2d62965ddb57fa4d6.ws.myminapp.com/ws/hydrogen/?x-hydrogen-client-id=a4d2d62965ddb57fa4d6&x-hydrogen-env-id=f1eeb28c9552d4c83df1&authorization=Hydrogen-r1%20580co4jlkysw7rprsgz4edxx9pubot3s';

    client = Client(
      realm: 'com.ifanrcloud',
      transport: WebSocketTransport(
        url,
        new Serializer(),
        WebSocketSerialization.SERIALIZATION_JSON,
      ),
    );

    Session session;

    try {
      session = await client.connect().first;
      String topic = 'com.ifanrcloud.schema_event.danmu_jiajun.on_create';

      // SubscribeOptions options = new SubscribeOptions();
      final subscription = await session.subscribe(topic);
      // subscription.eventStream.listen((event) => print(event.arguments[0]));
    } on Abort catch (abort) {
      print(abort.message.message);
    }
  }
}

I can't figure out what had happened here with the above errors. Kinda strange.

from connectanum-dart.

konsultaner avatar konsultaner commented on June 19, 2024

However, even when I execute the below code without an option, there are two errors shown:

  1. Unhandled Exception: Bad state: No element
  2. Unhandled Exception: NoSuchMethodError: The getter 'inMilliseconds' was called on null.

The exception 1 is thrown because the socket is closed by the router before the subscribe receives the subscription.

I don't get why exception is thrown, because there is a nullpointer check.. weired. I can't reproduce number 2 with my code.

[
        32,
        912873614,
        {
            "where": {}
        },
        "com.myapp.topic.emergency"
]

This is not possible at the moment. I could implement it.

from connectanum-dart.

jiajun-ifanr avatar jiajun-ifanr commented on June 19, 2024

Many thanks.

How did you know about the cause of exception 1? Is it possible to debug WebSocket via the package or could you recommend any methods (like Charles) to debug by myself? I've searched for answers with no hope for quite some time now.

Also, would it be good to throw an error message with better explanation about the exception rather than Bad state: No element since it might be confusing and I at first thought it was a Flutter issue.

from connectanum-dart.

konsultaner avatar konsultaner commented on June 19, 2024

@jiajun-ifanr I wrote a little unit test based on you code:

import 'package:connectanum/connectanum.dart';
import 'package:connectanum/json.dart';
import 'package:test/test.dart';

void main() {
  test("error", () async {
    await WampImplementation.subscribe();
  });
}

class WampImplementation {
  static void subscribe() async {
    String url =
        'wss://a4d2d62965ddb57fa4d6.ws.myminapp.com/ws/hydrogen/?x-hydrogen-client-id=a4d2d62965ddb57fa4d6&x-hydrogen-env-id=f1eeb28c9552d4c83df1&authorization=Hydrogen-r1%20580co4jlkysw7rprsgz4edxx9pubot3s';

    Client client = Client(
      realm: 'com.ifanrcloud',
      transport: WebSocketTransport(
        url,
        new Serializer(),
        WebSocketSerialization.SERIALIZATION_JSON,
      ),
    );

    Session session;

    try {
      session = await client.connect().first;
      String topic = 'com.ifanrcloud.schema_event.danmu_jiajun.on_create';

      // SubscribeOptions options = new SubscribeOptions();
      final subscription = await session.subscribe(topic);
      // subscription.eventStream.listen((event) => print(event.arguments[0]));
    } on Abort catch (abort) {
      print(abort.message.message);
    }
  }
}

You need to know that this package uses the transport streams and wraps a broadcast stream pre session around it. This way the broadcast stream may be closed safely and may be listened to multiple times.

session._openSessionStreamController.close();

You will see the transport stream is closed before the subscribe can receive a subscribed here:

This is not really an error but rather an event. You may have to subscribe to those events:

Completer get onConnectionLost => _onConnectionLost;

from connectanum-dart.

konsultaner avatar konsultaner commented on June 19, 2024

I only added custom options for subscriptions for now. If you need it for registrations too, plz open a new issue

from connectanum-dart.

jiajun-ifanr avatar jiajun-ifanr commented on June 19, 2024

Thanks a lot for your help!

I can now successfully subscribe to a topic and receive responses from our server.

But I still can't print out what the server has responded since the event.arguments (or anything in event) is always null:

SubscribeOptions options = new SubscribeOptions();
options.addCustomValue('where', (serializerType) => '{}');
final subscription = await session.subscribe(topic, options: options);
subscription.eventStream.listen((event) => print(event.arguments[0]));

It is believed that the server has sent valid information to a client (my JS client does receive it correctly) but I will have it checked with my colleagues.

I really appreciate your help. In what way I can buy you a cup of coffee?

from connectanum-dart.

konsultaner avatar konsultaner commented on June 19, 2024

But I still can't print out what the server has responded since the event.arguments (or anything in event) is always null:

I'll check that in a minute.

I really appreciate your help. In what way I can buy you a cup of coffee?

Never mind! 😄 A ⭐ is enough

from connectanum-dart.

konsultaner avatar konsultaner commented on June 19, 2024

@jiajun-ifanr I cannot debug it anymore, I get this error: Permission denied, please call login function first.

from connectanum-dart.

jiajun-ifanr avatar jiajun-ifanr commented on June 19, 2024

@konsultaner Please try and update the wss url with the following:

String url =
        'wss://a4d2d62965ddb57fa4d6.ws.myminapp.com/ws/hydrogen/?x-hydrogen-client-id=a4d2d62965ddb57fa4d6&x-hydrogen-env-id=f1eeb28c9552d4c83df1&authorization=Hydrogen-r1%20qlnol4csq38uzsne04wzd6y3684g1yy4';

event.arguments might only be printed out when the server responds. If you need me to send a response to the client, please let me know.

from connectanum-dart.

konsultaner avatar konsultaner commented on June 19, 2024

Could you send a respond every 1000ms?

from connectanum-dart.

jiajun-ifanr avatar jiajun-ifanr commented on June 19, 2024

@konsultaner Yes, now I send one every 3000ms

from connectanum-dart.

konsultaner avatar konsultaner commented on June 19, 2024

Your server has a wrong message encoding, the null values are wrong:

[36,844427229501,null,{},null,{"after":{"text":"this is example text","_read_perm":["user:anonymous"],"_write_perm":[],"created_at":1604311031,"updated_at":1604311031,"created_by":236097344815877,"id":"5f9fd7f7e9ed7d7681c837b4"},"before":{},"event":"on_create","schema_id":107144,"schema_name":"danmu_jiajun","id":"5f9fd7f7e9ed7d7681c837b4"}]

from connectanum-dart.

konsultaner avatar konsultaner commented on June 19, 2024

sorry. I see this should still work

from connectanum-dart.

jiajun-ifanr avatar jiajun-ifanr commented on June 19, 2024

@konsultaner Perfect! I can now make it work.

Thank you for all your support and kindness throughout the entire problem-solving process.

from connectanum-dart.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.