-
application.onConnect problem
Hi,
I am working on a many to one text chat app. I'm on win98
The logic that I want to implement is that if the "admin" has not logged in,
then, any user connecting would be shown a message and his connection
is to be rejected. But on running the app, my connection is getting
disconnected
even if I login as "admin". Other sample apps are working fine.
My client side coding is all right.
The following is what my main.asc has
application.onAppStart = function() {
// Get the server shared object 'users_so'
application.users_so = SharedObject.get("users_so", true);
// Initialize the unique user ID
application.uniqueID = 0;
};
application.onConnect = function(clientObj, userName) {
if (userName != "admin") {
var adminFound = 0;
for (var k = 0; k < application.clients.length; k++) {
if (application.clients[k].name == "admin") {
adminFound = 1;
}
}
if (adminFound == 0) {
var errObj = new Object();
errObj.message = "Admin not logged in."
application.rejectConnection(clientObj, errObj);
}
}
// accept the connection and update the sharedObject
// code goes here
};
;
application.onDisconnect = function(client) {
// disconnect code goes here
}
Can anybody plz point out where I am going wrong.
Any and all help is deeply appreciated.
Thanks
Vinod
-
Hey -
I'm guessing it's because in this bit of code:
Code:
application.onConnect = function(clientObj, userName) {
if (userName != "admin") {
var adminFound = 0;
for (var k = 0; k < application.clients.length; k++) {
if (application.clients[k].name == "admin") {
adminFound = 1;
}
}
if (adminFound == 0) {
var errObj = new Object();
errObj.message = "Admin not logged in."
application.rejectConnection(clientObj, errObj);
}
}
};
You're only dealing with "non-admin" clients. You need to add an else in there, so that if you are the admin, it accepts your connection.
Code:
application.onConnect = function(clientObj, userName) {
if (userName != "admin") {
var adminFound = 0;
for (var k = 0; k < application.clients.length; k++) {
if (application.clients[k].name == "admin") {
adminFound = 1;
}
}
if (adminFound == 0) {
var errObj = new Object();
errObj.message = "Admin not logged in."
application.rejectConnection(clientObj, errObj);
}
} else {
//user must be admin
application.acceptConnection(clientObj, userName);
}
};
That should do it!
Psx
-
Thanks psychlonex
I tried it. No luck friend.
But one doubt. If the user IS "admin", then should'nt the
'if(userName != "admin"){}' block be skipped and the connection accepted. I mean is'nt the 'else' part assumed?
I changed the code and got it working. There could be better ways but for the time being I'm happy with it.
application.onConnect = function(clientObj, userName)
{
var adminFound = 0;
clientObj.name = userName;
// Accept the client's connection
application.acceptConnection(clientObj);
if(userName != "admin")
{
for (var i = 0; i < application.clients.length; i++){
if(application.clients[i].name == "admin"){
var adminFound = 1;
}
}
if(adminFound == 0){
application.disconnect(clientObj);
}
}
}
Vinod
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|