I'm trying to fix a problem that I'm having with a chatbox I'm creating,
So i have a chatbox system fully working
If i open 2 swf files for my chatbox within 3 seconds of each other and join the room everything is working fine,
if i open another one and join the room it doesn't show the other 2 users as connected it only shows the 3rd user id, but if the 3rd user types a message the 1st and 2nd users can see it (they can also see the 3rd user on the user online list) . but if they 1st and 2nd user type a message the 3rd user doesn't get the message
I know it's hard to understand but please bare with me
here is the as3 chat.as code (it's slightly modified)
- Code: Select all
package sample.ui{
import flash.display.Sprite
import flash.display.Stage
import flash.events.Event
import flash.text.TextFormatAlign
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.*;
import flash.net.URLRequest;
import flash.events.MouseEvent
import flash.events.KeyboardEvent
import sample.ui.components.*
import sample.ui.components.scroll.*
import playerio.*;
public class Chat extends Sprite{
private var _connection:Connection
private var _stage:Stage
private var base:Box
private var input:Input;
private var content:Rows
private var container:ScrollBox
private var userlist:Rows
private var users:Object = {};
function Chat(stage:Stage, connection:Connection){
this._stage = stage
this._connection = connection
base = new Box().minSize(900,27).margin(0x0,5,5,5).add(
new Box().fill(0x0,.5,10).margin(5,5,5,5).add(
new Box().margin(0,0,NaN,0).add(
new Box().minSize(0,150).fill(0xffffff,1,3).margin(0,0,0,0).add(
new ScrollBox().margin(2,2,2,2).add(
userlist = new Rows().spacing(1)
)
)
)
).add(
new Box().margin(155,0,32,0).add(
new Box().fill(0xffffff,1,3).margin(0,0,0,0).add(
container = new ScrollBox().margin(2,2,2,2).add(
content = new Rows().spacing(1)
)
)
)
).add(
new Box().margin(NaN,0,0,0).add(
new Box().minSize(0,27).fill(0xffffff,1,3).margin(3,3,3,3).add(
new Box().margin(0,60,0,0).add(
new Box().margin(2,0,0,3).fill(0x666666,0,5).border(1,0x444444,1).add(
input = new Input("").setStyle(false,false)
)
)
).add(
new Box().margin(0,0,0,NaN).add(
new Box().minSize(55,0).margin(0,0,0,0).add(
new TextButton("Send",sendChatText)
)
)
)
)
)
);
addChild(base);
_connection.addMessageHandler("ChatInit", onInit)
_connection.addMessageHandler("ChatMessage", onMessage)
_connection.addMessageHandler("ChatJoin", function(m:Message, id:String, name:String){
addUser(id,name)
})
_connection.addMessageHandler("ChatLeft", function(m:Message, id:String){
removeUser(id)
})
input.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent){
if(e.keyCode == 13){
sendChatText();
}
})
stage.addChild(this);
_stage.addEventListener(Event.RESIZE, handleResize)
handleResize();
}
public function sendMessage(message:String){
if(_connection.connected && message != ""){
_connection.send("ChatMessage", message)
}
}
private function sendChatText():void{
sendMessage(input.text)
input.clear();
}
private function onInit(m:Message, id:String){
for( var a:int=1;a<m.length;a+=2){
addUser(m.getString(a),m.getString(a+1))
}
}
private function addUser(id:String, username:String){
if(!users[id]){
users[id] = new Label(username, 12, TextFormatAlign.LEFT, 0x0, true)
userlist.addChild(users[id])
base.reset();
var snd:Sound = new Sound();
snd.load(new URLRequest("my.mp3"));
snd.play();
}
}
private function removeUser(id:String){
if(users[id]){
userlist.removeChild(users[id])
delete users[id]
base.reset();
}
}
private function onMessage(m:Message, id:String, message:String){
if(content.numChildren > 50){
content.removeChild( content.getChildAt(0) );
}
content.addChild(
new Label(users[id].text + ": " + message, 12, TextFormatAlign.LEFT, 0x0, true)
)
base.reset();
container.scrollY = 100000
}
private function handleResize(e:Event = null){
base.height = _stage.stageHeight
base.width = 250;
base.x = _stage.stageWidth - 1000
}
}
}