I am working on Lan based interoffice messaging system.
The steps in my applications are :
Server starts and listen for clients
Clients on connection gets the view of all the other connected clients.
On connection to the server i do check in the database that if that client is authorize or not.
if i dont check in the database on client connection to the server then the application does work normally other wise the client if not exists in the database the the client application got closed.
the related code is here :
public void CheckUserName(string userName)
{
if (userName != "Usman") // checking in database( a static name)
{
//Check if the username is registered
Send("sorry#Invalid Username, try another Username!!");
Disconnect();
return;
}
else
{
//If name is not duplicate then the client is connected
this.connected =true;
this.userName =userName;
//Build the Usernames list and send it to the client
StringBuilder userList = new StringBuilder();
userList.Append(this.clientID);
Hashtable clientTable =ClientList.GetList;
foreach(DictionaryEntry d in clientTable)
{
//Seperate the usernames by a '#'
userList.Append("#");
userList.Append(d.Value.ToString());
}
//Start the llistening
lock(myClient.GetStream())
{
AsyncCallback GetStreamMsgCallback = new AsyncCallback(GetStreamMsg);
myClient.GetStream().BeginRead(recByte,0,1024,GetStreamMsgCallback,null);
}
//Send the Userlist
Send(userList.ToString());
//Raise the Connected Event
if(Connected!=null)
{
EventArgs e = new EventArgs();
Connected(this, e);
}
}
}
Do anyone could suggest that what to do to get rid of this bad thing?
Have you considered initially querying the database for all enabled users when the server starts then caching those results? this way you can avoid having to query on every user connection. You can store the entitled users in a Dictionary or some other style of lookup table.
Related
I need to submit a custom Remote id when connecting to a IKEv2-VPN created with DotRas but I haven't figured out any way of doing this.
Remote id is submitted, however it's just the IP that is submitted. I need to customize this.
This is the code that creates the connection:
private void CreateConnection(String connectionName)
{
this.rasPhoneBook.Open(PHONEBOOK_PATH);
RasEntry entry = RasEntry.CreateVpnEntry(connectionName, SERVER_ADDRESS,
RasVpnStrategy.IkeV2Only,
RasDevice.Create(connectionName, RasDeviceType.Vpn));
this.rasPhoneBook.Entries.Add(entry);
}
And this is where I connect:
private void Connect()
{
// disconnect any lingering connections before connecting
this.Disconnect();
this.rasDialer.EntryName = this.serviceNameTextBox.Text;
this.rasDialer.PhoneBookPath = PHONEBOOK_PATH;
this.rasDialer.Credentials = new NetworkCredential(this.usernameTextBox.Text,
this.passwordTextBox.Text);
this.rasDialer.DialAsync();
}
Thanks for any help solving this.
When I try to broadcast a message to all clients, I can trigger client's javascript code from server and get the job done.
But this time my aim is to trigger a method in all servers. For example, when roles of a user changed in one server, I want to warn other servers about this operation and I want to make other servers retrieve updated user role list for particular user.
Is it possible to do this with SignalR? Can a server behave like a client (browser)?
Yes you can do that.
Let's say you have the following hub:
public class TheHub : Hub
{
public void RoleChanged(int userId)
{
Clients.All.roleChanged(userId);
}
}
On all the listening servers, you'd have to do:
var _connection = new HubConnection("http://localhost:1234/signalr");
var _theHub = _connection.CreateHubProxy("TheHub");
_myHub.On<int>("RoleChanged", userId =>
{
System.Diagnostics.Debug.WriteLine("Changed user's Id: " + userId);
});
_connection.Start().Wait();
To invoke the RoleChanged event, do:
_myHub.Invoke("RoleChanged").Wait();
i have stored some client data on server in datagrid using signalr (whenever client connects details of all clients updated on server like ipaddress, name etc)... so i want to send that datagrid details to all clients and the condition is whenever new clients connect to server then all client including current client must get updated list ....here is my code basically what i have done till now,
public override Task OnConnected()
{
object ipaddress;
var a=Context.QueryString["name"];
var b= Context.QueryString["AnotherValue"];
if (Context.Request.Environment.TryGetValue("server.RemoteIpAddress", out ipaddress))
{
//ipcollections = new List<string[]>();
userhandler.ipcol.Add(new string[] { ipaddress.ToString(), a, b });
Program.MainForm.writetodatagrid(userhandler.ipcol);
}
Program.MainForm.WriteToConsole("Client connected: " + Context.ConnectionId );
return base.OnConnected();
}
and showing this list on server itself in datagird...i have to send this list to all clients...please help me...thank you....or is there any other way or am i doing things wrong please tell me..
On the server you would have a Hub and a method on the Hub to broadcast.
public class MyHub : Hub
{
public void Send(string ipaddress, string name)
{
Clients.All.addMessage(ipaddress, name);
}
}
Take a look at the following post. It has a an example of what you would do on your winforms client.
https://code.msdn.microsoft.com/windowsdesktop/Using-SignalR-in-WinForms-f1ec847b#content
and the source code for the winforms client:
https://code.msdn.microsoft.com/windowsdesktop/Using-SignalR-in-WinForms-f1ec847b/sourcecode?fileId=119892&pathId=583880341
I'm trying to get the connected user id in my signleR application but I didn't manage to find it. Is there any way to get it because I want when two users are chatting just the two of them will receive each other's messages and not the whole group they are connected to, or all clients connected to signleR hub. My main goal is to send private messages between two connected clients.
My code is so complicated but all I have found is how to broadcast my message to all the clients or to a specific group, but I couldn't manage to send a private message. I did this to send user typing message:
public void UserTyping(string msg, int ToClient) { // display User is Typing Msg
Clients.Others.OtherUserIsTyping(msg, ToClient);
}
ClientSide:
var keyPressCount = 0;
$("#<%=MsgToSend.ClientID %>").keypress(function () {
// Throttle the server call with some logic
// Don't want to call the server on every keypress
var ToClientID = $("#ToClientID").val();
if (keyPressCount++ % 10 == 0) {
chat.server.userTyping("Is typing...", ToClientID); // call the server side function
}
});
chat.client.OtherUserIsTyping = function (msg, ToClient) { // declare the function on the chat hub so the server can invoke it
$("#UserTyping").html(msg);
}
I want to create html5 web multiroom chat, based on HTML5 websocket.
But I need some little help to start.
I want to do server side code in c#, but I can not find any tutorials how to do chat websocket server with multi room in c#.
Is there any server which is already implemented in .net, or which I can update to multi room chat ?
It is a little project, one room for 10 peoples. Could you help to me how to start ?
Thank you very much !
I prepare example code structure:
Main server class:
class Program
{
// List of courses, which are currentli avalible ( REPRESENT CHAT ROOM)
protected static ConcurrentDictionary<Course, string> OnlineUsers = new ConcurrentDictionary<Course, string>();
static void Main(string[] args)
{
// Initialize the server on port 81, accept any IPs, and bind events.
var aServer = new WebSocketServer(81, IPAddress.Any)
{
OnReceive = OnReceive,
OnSend = OnSend,
OnConnected = OnConnect,
OnDisconnect = OnDisconnect,
TimeOut = new TimeSpan(0, 5, 0)
};
aServer.Start();
// Accept commands on the console and keep it alive
var command = string.Empty;
while (command != "exit")
{
command = Console.ReadLine();
}
aServer.Stop();
}
// event when the clients connect to server
// Server send to client list of Lessons which are avalible, after
private static void OnConnect(UserContext context)
{
throw new NotImplementedException();
}
// event whent the client, want to disconnect from server
private static void OnDisconnect(UserContext context)
{
throw new NotImplementedException();
}
// event, when client is sending some data
private static void OnSend(UserContext context)
{
throw new NotImplementedException();
}
// event, when server receive data from client
// client choose which room want to join and, we add cleint to list of lessons which he choose
// another method ... Register, Rename, LogOff ...
private static void OnReceive(UserContext context)
{
throw new NotImplementedException();
}
}
Course class: (ROOMS)
class Course
{
// Every course has list of active users
protected static ConcurrentDictionary<User, string> OnlineUsers = new ConcurrentDictionary<User, string>();
// Name of course
public String CourseName { get; set; }
}
User class:
class User
{
// Name of User
public string Name = String.Empty;
// UserContext - Contains data we will export to the Event Delegates.
public UserContext Context { get; set; }
}
It is good structure for my purpose ? I have many courses (room), with one teacher, in one course can be 20 pupils example .. In one course the pupils can talk with techer using chat (web socket) and drawing board ..
That's how I would build the object hierarchy:
The chat server should have a list of ChatRooms.
Each ChatRoom should have a list of ChatUsers.
Each ChatUser should have one or no ChatRoom and an outbound socket.
(this assumes that a user is only in one room at a time. Allowing multiple rooms would make things a bit more complex)
That's how room selection could work:
When a client connects, a ChatUser is created. The first thing the server does is send the list of chatrooms to the user. The client then responds with the name of the chatroom it wants to join. When a chatroom of that name doesn't exist, it is created and added to the global list of rooms.
The client is then added to the room, and the room is set on the client.
That's how chatting could work:
When the socket of the user receives a chat message, it should call a SendToAllClients method on the room the user is currently in (when the room is null, it should return an error message to the user that they must join a room first).
The SendToAll method of the room should then call a SendToClient of all users which are on its list of users.
The SendToClient method of the class would then send the chat message to the client.
How to expand this for multiple chatrooms per user
To allow a client to join multiple chatrooms at once and have separate conversations in them, the client must be able to:
request a list of rooms at any time, not just at startup
join rooms at any time, not just at startup
leave rooms
specify the room when sending a message
That means that the action the client wants to perform can not be deduced from the state it is currently in. You need to add this information to the messages of the user. You could, for example, do this as prefixes. Like
!list
requests the list of rooms
!join:asdf
join/create the room asdf
_asdf:Hello
sends the message Hello to the room asdf.
The messages from the server should have similar prefixes, so that the client can deduce if a message is a room list or a chat message and from what room it originates.
You should try to look into SignalR for ASP.NET (example : jabbr.net/). This may be more helpful and handy.