Extension Users + Raw Protocol

Need help with SmartFoxServer? You didn't find an answer in our documentation? Please, post your questions here!

Moderators: Lapo, Bax

Post Reply
Dan0
Posts: 16
Joined: 22 Sep 2006, 20:39

Extension Users + Raw Protocol

Post by Dan0 »

Hi!
- Im making an extension so that when data is sent to the server and the cmd is "mv" it will send that data on to everyone else in that room. The extension is running in the zone and there is 1 room in the zone (soon to be more).
- My problem is that I can not work out exactly how to find all the users in the room.
Here is my code:

Code: Select all

function handleRequest(cmd, params, user, fromRoom, proto) {
	if (cmd == "mv") {
		var m = [];
		m[0] = cmd;
		m[1] = params[0];
		m[2] = params[1];
		m[3] = params[2];
		_server.sendResponse(m, <from room or somthing?>, user, <all other users in room>, "str");
	}
}
- As you can see I am using raw protocol because it is an fps. I tried making it using sendObject which worked but was very laggy so my only alternative is to make an extension using raw protocol.
- I think everything else in my code is working properly all I need is to find the room that the response is coming from, all the users in that room, so I can send the information to all the users in the room.
- Also what is raw protocol and why is it less laggy (if any) than sendObject, are there alternatives?
- What is a good time delay for each command for an FPS? 20ms?

Thanks for your help,
Dan
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

hi,
fromRoom is the id of the room where the request is coming from. So if you want to send data to the people in that room, just use that variable.

Finding all other users in the room can be done with a function like this:

Code: Select all

/**
	Return a list of all the other users in the room except me
	
	@param room		the room
	@param me		the user excluded from the list
*/	
function getAllUsersButMe(room, me)
{
	var uList = []
	var allUsers = room.getAllUsers()
	var u = null
	var meId = me.getUserId()
	
	for (var i in allUsers)
	{
		u = allUsers[i]
		
		if (u.getUserId() != meId)
			uList.push(u)
	}
	
	return uList
}
Just pass the room object (room) and the user object that should be excluded (me)
- Also what is raw protocol and why is it less laggy (if any) than sendObject, are there alternatives?
It is less laggy because it sends a lot less data. The sendObject sends more data because it serializes the objects you pass in XML, preserving its structure and data types of all properties.

The raw string protocol is less sophisticated, doesn't add serialization/deserialization but it is usually 5-10 times smaller :)
- What is a good time delay for each command for an FPS? 20ms?
Wow, 20ms would be uber-good! Unfortunately 20ms is a value that you can obtain in a LAN, but it's very hard to get over the internet.
Excellent lag times are those between 40-80ms, good ones can be between 80-180 etc...
Lapo
--
gotoAndPlay()
...addicted to flash games
Dan0
Posts: 16
Joined: 22 Sep 2006, 20:39

Post by Dan0 »

So do I need to define the fromRoom in the response or should I leave it as -1?

Code: Select all

_server.sendResponse(m, -1, user, uList, "str");
And if I add the function I need to call it by:

Code: Select all

getAllUsersButMe(fromRoom, user) 
(from within the handRequest() function)

Also you explained "lag" to me which I already know with roundTrip() :D. What I needed to know is how long between each message each player sends to the server should be for a smooth movement (without tweening). E.g. Each time the player moves it checks if 50ms has passed since the last message. Should it be 50ms or lower or higher?

Thanks for the great response you really cleared out some issues!
Dan
Dan0
Posts: 16
Joined: 22 Sep 2006, 20:39

Post by Dan0 »

When I try it I just get a load of errors in admin:
Error in extension [ polyEngine.as ]: TypeError: Cannot find function getAllUsers. (polyEngine.as#1007) Line: 18
WHY?!!?!?
Virusescu
Posts: 260
Joined: 07 Sep 2005, 09:36
Location: [RO]Bucharest
Contact:

Post by Virusescu »

getAllUsers() is a method of the Room Class
In order to get all users from a room you must call it like

Code: Select all

var allUsersArray = _server.getCurrentRoom().getAllUsers()
or

Code: Select all

var allUsersArray = _server.getCurrentZone().getRoom(someId).getAllUsers()
and not just getAllUsers() because SFS will search for a function defined in that extension. It might work if you wrote one there... but as the error shows it, you don't :).
function onJoin(usr) {if (usr.getName() == "Lapo") trace ("All Hail Lapo");}
Dan0
Posts: 16
Joined: 22 Sep 2006, 20:39

Post by Dan0 »

Thanks a lot! Last thing i need to know is when you send response and put in define the fromUser what should you put in it?

function handleRequest(cmd, params, user, fromRoom, proto) {
_server.sendResponse(m, -1, user, userList, "str");
}
}

is what i currently have but what should I put? Also when it is recieved in flash where in the resObj is it??? I cant find it, maybe because its undefined but I would have expected it to be resObj[5] or somthing?

Thanks for the help again,
Dan[/code]
Post Reply