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

Message parameter in event.on('message', chatController.newMessage); always receive as null value #63

Closed
WissamALSbenaty opened this issue May 26, 2024 · 7 comments
Labels
bug Something isn't working

Comments

@WissamALSbenaty
Copy link

WissamALSbenaty commented May 26, 2024

I am trying to run my first vania server

my main focus is testing how websocket works
so I discovered that I need to change the value of websocket in both .env file and app config file to true after taking a long time

but the problem for me now I print the value of the message that the client sent

web_socket.dart file

import 'package:vania/vania.dart';
import 'package:trix_backend/app/http/controllers/ws/chat_web_socket_controller.dart';

class WebSocketRoute implements Route {
  @override
  void register() {
    Router.websocket('/ws', (WebSocketEvent event) {
      event.on('message', chatController.newMessage);
    });
  }
}

chat_web_socket_controller.dart

import 'package:vania/vania.dart';

class ChatWebSocketController extends Controller {
  Future newMessage(WebSocketClient client, dynamic message) async {
    print(' recieved $message');
    client.toRoom('toRoom', "1", "test");
  }

}

ChatWebSocketController chatController = ChatWebSocketController();

the events

{
    "event":"joinRoom",
    "room":"1"
}
{
    "event":"message",
    "data":"1"
}

the output recieved null
and the toRoom does nothing which is another problem , emit and broadcast worked fine with me

@WissamALSbenaty WissamALSbenaty added the bug Something isn't working label May 26, 2024
@WissamALSbenaty
Copy link
Author

i found that the payload must be under "payload" key instead of "data" as mentioned in the documentation

@WissamALSbenaty
Copy link
Author

still toRoom Function is not working .. it seems that joinRoom is not working fine , I have printed the members in webSocketClientImpl and always returns an empty array

javad-zobeidi added a commit to javad-zobeidi/framework that referenced this issue May 27, 2024
javad-zobeidi added a commit that referenced this issue May 27, 2024
Fixed websocket issue(#63) join and left room
@javad-zobeidi
Copy link
Collaborator

javad-zobeidi commented May 27, 2024

Thank you for reporting, fixed in version 0.2.2

please update

To join a room use this

{
    "event":"join-room",
    "room":"unique room name or ID"
}

To send a message to a room

{
    "event":"message",
    "payload":{
        "room":"room name or id",
        "message": "message"
    }
}

Controller

import 'package:vania/vania.dart';

class ChatWebSocketController extends Controller {
  Future newMessage(WebSocketClient client, dynamic payload) async {
    client.toRoom('message', payload['room'].toString(), payload['message']);
  }
}

ChatWebSocketController chatController = ChatWebSocketController();

@WissamALSbenaty
Copy link
Author

WissamALSbenaty commented Jun 9, 2024

I am still unable to join a room or print anything to verify
all I got is {"event":"connect"}

{
    "event":"join-room",
    "room":"myRoom"
}
{
    "event":"play",
    "payload":{
        "room":"myRoom",
        "message": "hehe"
    }
}
import 'package:vania/vania.dart';
import 'package:trix/app/http/controllers/ws/chat_web_socket_controller.dart';

class WebSocketRoute implements Route {
  @override
  void register() {
    Router.websocket('/ws', (WebSocketEvent event) {
      event.on('play', chatController.joinGame);
    });
  }
}

import 'dart:convert';

import 'package:vania/vania.dart';


class ChatWebSocketController extends Controller {

  List<String> gamePlayers=[];
  Future joinGame(WebSocketClient client, dynamic message) async {
    print('new event ');
    gamePlayers.add(client.clientId);
    client.broadcast('new player', json.encode({
      "game players":gamePlayers.length,
    }));
  }
}

ChatWebSocketController chatController = ChatWebSocketController();

@javad-zobeidi

@javad-zobeidi
Copy link
Collaborator

Hi @WissamALSbenaty please test with this

Dart client code

import 'dart:convert';
import 'package:web_socket_channel/web_socket_channel.dart';
void main() async {
  final wsUrl = Uri.parse('ws://127.0.0.1:8000/ws');
  final channel = WebSocketChannel.connect(wsUrl);

  await channel.ready;

  channel.stream.listen((message) {
    print('message $message');
  });

channel.sink.add(jsonEncode({"event": "join-room", "room": "myRoom"}));

  Future.delayed(Duration(seconds: 2), () {
    channel.sink.add(jsonEncode({
      "event": "message-to-room",
      "payload": {"room": "myRoom", "message": "Javad Join Room"}
    }));
  });
}

Server Code

// Router

import 'package:vania/vania.dart';
import 'package:server/app/http/controllers/ws/chat_web_socket_controller.dart';

class WebSocketRoute implements Route {
  @override
  void register() {
    Router.websocket('/ws', (WebSocketEvent event) {
      event.on('message', chatController.newMessage);
      event.on('message-to-room', chatController.joinRoom);
    });
  }
}
// Controller

import 'package:vania/vania.dart';

class ChatWebSocketController extends Controller {
  Future newMessage(WebSocketClient client, dynamic message) async {
    
  }

  Future joinRoom(WebSocketClient client, dynamic payload) async {
    client.toRoom('message-to-room', payload['room'], payload['message']); // send the message to the room
  }

}

ChatWebSocketController chatController = ChatWebSocketController();

@WissamALSbenaty
Copy link
Author

WissamALSbenaty commented Jun 9, 2024

same result
@javad-zobeidi

@javad-zobeidi
Copy link
Collaborator

The code I sent has been tested and is working. Could you please provide your cod?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants