Forum ActionScript 3.0 Not receiving message

Problems and discussions relating to ActionScript 3.0 here.

Not receiving message

Postby UniversalGames » September 20th, 2012, 4:17 pm

Hello,

i'm trying to send the mouseY position with
Code: Select all
conn.send("moveY", mouseY);

and recieve it with
Code: Select all
connection.addMessageHandler("moveY", function(m:Message, yC:int){
   p1.y = yC;
})


i set conn = connection in the handleJoin function and the problem is that i don't recieve any messages, i can see that the bandwidth is increasing when i move the cursor so i guess it's sending stuff to the server. I can see messages when someone connects and the hello message from the server etc but anything i send does not make it.

Here is the full code:
Code: Select all
package {
   import flash.display.MovieClip
   import playerio.*
   import flash.events.MouseEvent;
   
   public class MyGame extends MovieClip{
      
      var conn:Connection;
      
      function MyGame(){
         stop();
         PlayerIO.connect(
            stage,                        //Referance to stage
            "my game id",         //Game id (Get your own at playerio.com)
            "public",                     //Connection id, default is public
            "GuestUser",                  //Username
            "",                           //User auth. Can be left blank if authentication is disabled on connection
            null,                        //Current PartnerPay partner.
            handleConnect,                  //Function executed on successful connect
            handleError                     //Function executed if we recive an error
);
      }
      
      
      private function handleConnect(client:Client):void{
         trace("Sucessfully connected to player.io");
         
         //Set developmentsever (Comment out to connect to your server online)
         client.multiplayer.developmentServer = "my ip:8184";
         
         //Create pr join the room test
         client.multiplayer.createJoinRoom(
            "test",                        //Room id. If set to null a random roomid is used
            "MyCode",                     //The game type started on the server
            true,                        //Should the room be visible in the lobby?
            {},                           //Room data. This data is returned to lobby list. Variabels can be modifed on the server
            {},                           //User join data
            handleJoin,                     //Function executed on successful joining of the room
            handleError                     //Function executed if we got a join error
         );
      }
      
      
      private function handleJoin(connection:Connection):void{
         trace("Sucessfully connected to the multiplayer server");
         gotoAndStop(2);
         conn = connection;
         
         
         //Add disconnect listener
         connection.addDisconnectHandler(handleDisconnect);
               
         //Add listener for messages of the type "hello"
         connection.addMessageHandler("hello", function(m:Message){
            trace("Recived a message with the type hello from the server");         
         })
         
         //Add message listener for users joining the room
         connection.addMessageHandler("UserJoined", function(m:Message, userid:uint){
            trace("Player with the userid", userid, "just joined the room");
         })
         
         //Add message listener for users leaving the room
         connection.addMessageHandler("UserLeft", function(m:Message, userid:uint){
            trace("Player with the userid", userid, "just left the room");
         })
         
         connection.addMessageHandler("moveY", function(m:Message, yC:Number){
            trace("recieved:"+m+":"+yC);
            p1.y = Number(m);
         })
         
         //Listen to all messages using a private function
         connection.addMessageHandler("*", handleMessages)
   
         stage.addEventListener(MouseEvent.MOUSE_MOVE, moveP);
         
      }
      
      private function moveP(Event:MouseEvent):void{
         p2.y = mouseY;
         conn.send("hello");
         conn.send("moveY", mouseY);
      }
      
      private function handleMessages(m:Message){
         trace("Recived the message", m)
      }
      
      private function handleDisconnect():void{
         trace("Disconnected from server")
      }
      
      private function handleError(error:PlayerIOError):void{
         trace("got",error)
         gotoAndStop(3);

      }
   }   
}



Thank you for your help, i really appreciate it!
UniversalGames
 
Posts: 2
Joined: March 28th, 2012, 5:25 pm

Re: Not receiving message

Postby Benjaminsen » September 20th, 2012, 8:36 pm

What code do you have on your server?
Benjaminsen
.IO
 
Posts: 1444
Joined: January 12th, 2010, 11:54 am
Location: Denmark

Re: Not receiving message

Postby UniversalGames » September 21st, 2012, 12:17 am

Benjaminsen wrote:What code do you have on your server?


I'm using the standard "NewGame" server.

Here is the source:

Code: Select all
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using PlayerIO.GameLibrary;
using System.Drawing;

namespace MyGame {
   public class Player : BasePlayer {
      public string Name;
   }

   [RoomType("MyCode")]
   public class GameCode : Game<Player> {
      // This method is called when an instance of your the game is created
      public override void GameStarted() {
         // anything you write to the Console will show up in the
         // output window of the development server
         Console.WriteLine("Game is started: " + RoomId);

         // This is how you setup a timer
         AddTimer(delegate {
            // code here will code every 100th millisecond (ten times a second)
         }, 100);
         
         // Debug Example:
         // Sometimes, it can be very usefull to have a graphical representation
         // of the state of your game.
         // An easy way to accomplish this is to setup a timer to update the
         // debug view every 250th second (4 times a second).
         AddTimer(delegate {
            // This will cause the GenerateDebugImage() method to be called
            // so you can draw a grapical version of the game state.
            RefreshDebugView();
         }, 250);
      }

      // This method is called when the last player leaves the room, and it's closed down.
      public override void GameClosed() {
         Console.WriteLine("RoomId: " + RoomId);
      }

      // This method is called whenever a player joins the game
      public override void UserJoined(Player player) {
         // this is how you send a player a message
         player.Send("hello");

         // this is how you broadcast a message to all players connected to the game
         Broadcast("UserJoined", player.Id);
      }

      // This method is called when a player leaves the game
      public override void UserLeft(Player player) {
         Broadcast("UserLeft", player.Id);
      }

      // This method is called when a player sends a message into the server code
      public override void GotMessage(Player player, Message message) {
         switch(message.Type) {
            // This is how you would set a players name when they send in their name in a
            // "MyNameIs" message
            case "MyNameIs":
               player.Name = message.GetString(0);
               break;
         }
      }

      Point debugPoint;

      // This method get's called whenever you trigger it by calling the RefreshDebugView() method.
      public override System.Drawing.Image GenerateDebugImage() {
         // we'll just draw 400 by 400 pixels image with the current time, but you can
         // use this to visualize just about anything.
         var image = new Bitmap(400,400);
         using(var g = Graphics.FromImage(image)) {
            // fill the background
            g.FillRectangle(Brushes.Blue, 0, 0, image.Width, image.Height);

            // draw the current time
            g.DrawString(DateTime.Now.ToString(), new Font("Verdana",20F),Brushes.Orange, 10,10);

            // draw a dot based on the DebugPoint variable
            g.FillRectangle(Brushes.Red, debugPoint.X,debugPoint.Y,5,5);
         }
         return image;
      }

      // During development, it's very usefull to be able to cause certain events
      // to occur in your serverside code. If you create a public method with no
      // arguments and add a [DebugAction] attribute like we've down below, a button
      // will be added to the development server.
      // Whenever you click the button, your code will run.
      [DebugAction("Play", DebugAction.Icon.Play)]
      public void PlayNow() {
         Console.WriteLine("The play button was clicked!");
      }

      // If you use the [DebugAction] attribute on a method with
      // two int arguments, the action will be triggered via the
      // debug view when you click the debug view on a running game.
      [DebugAction("Set Debug Point", DebugAction.Icon.Green)]
      public void SetDebugPoint(int x, int y) {
         debugPoint = new Point(x,y);
      }
   }
}



EDIT: i see the problem now when i looked into the server code. So another question is how i broadcast a message to all users except the person who sent it?

EDIT 2: Solved it, thanks for pointing me in the right direction!
UniversalGames
 
Posts: 2
Joined: March 28th, 2012, 5:25 pm

Re: Not receiving message

Postby Benjaminsen » September 21st, 2012, 8:39 am

Pleasure :)
Benjaminsen
.IO
 
Posts: 1444
Joined: January 12th, 2010, 11:54 am
Location: Denmark

Re: Not receiving message

Postby wgfunstorm » September 24th, 2012, 5:10 am

UniversalGames wrote:So another question is how i broadcast a message to all users except the person who sent it?

EDIT 2: Solved it, thanks for pointing me in the right direction!


Not sure if the solved it was in reference to your previous question, or the one for the edit. Just in case you're still stuck on this, I don't know of a way to broadcast a message to everyone except the sender, but I use this function to send a message to all players except the sender:

Code: Select all
        private void sendMessageToAllOtherPlayers(Player player, Message message)
        {
            foreach (Player p in Players)
            {
                if (p.Id != player.Id)
                {
                    p.Send(message);
                }
            }
        }
wgfunstorm
 
Posts: 16
Joined: January 21st, 2012, 4:10 pm

Re: Not receiving message

Postby Benjaminsen » September 24th, 2012, 5:49 pm

The multiplayer system provides the convenience method called Broadcast as well
Benjaminsen
.IO
 
Posts: 1444
Joined: January 12th, 2010, 11:54 am
Location: Denmark

Re: Not receiving message

Postby wgfunstorm » September 24th, 2012, 6:14 pm

Did you see that he doesn't want to forward the message to the original sender? Can this be done with broadcast? I didn't see a way.
wgfunstorm
 
Posts: 16
Joined: January 21st, 2012, 4:10 pm

Re: Not receiving message

Postby Benjaminsen » September 24th, 2012, 6:25 pm

wgfunstorm wrote:Did you see that he doesn't want to forward the message to the original sender? Can this be done with broadcast? I didn't see a way.


I did not, sorry :)
Benjaminsen
.IO
 
Posts: 1444
Joined: January 12th, 2010, 11:54 am
Location: Denmark

Re: Not receiving message

Postby limjesica » August 17th, 2022, 8:21 am

Thanks for your post! Can this be done with broadcasting?
moviedle quordle
limjesica
 
Posts: 1
Joined: August 17th, 2022, 8:19 am


Return to ActionScript 3.0