-
Notifications
You must be signed in to change notification settings - Fork 8
Added recognition of the client and recording his action. #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| using Microsoft.AspNet.SignalR; | ||
|
|
||
| namespace SignalR2.Extensions | ||
| { | ||
| public static class ExtensionHttpContext | ||
| { | ||
| public static string GetRemoteIpAddress(this IRequest request) | ||
| { | ||
| object ipAddress; | ||
| if (request.Environment.TryGetValue("server.RemoteIpAddress", out ipAddress)) | ||
| { | ||
| return ipAddress as string; | ||
| } | ||
| return ""; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,65 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Data; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using System.Web; | ||
| using Microsoft.AspNet.SignalR; | ||
| using Microsoft.AspNet.SignalR.Hubs; | ||
| using SignalR2.Extensions; | ||
|
|
||
| namespace SignalR2.Hubs | ||
| { | ||
| [HubName("puns")] | ||
| public class PunsHub : Hub<IPunsClientHandler> | ||
| { | ||
| static List<string> Image =new List<string>(); | ||
| public static List<string> Images = new List<string>(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Danger! Can be cross thread called. Use the thread safe collections. |
||
| public static UsersHandler Users = new UsersHandler(); | ||
|
|
||
| public override Task OnConnected() | ||
| { | ||
| Clients.Caller.LoadImage(Image); | ||
| AddClient(Context.ConnectionId,GetIpAddress()); | ||
| Clients.Caller.LoadImage(Images); | ||
| return base.OnConnected(); | ||
| } | ||
|
|
||
| public override Task OnDisconnected(bool stopCalled) | ||
| { | ||
| RemoveClient(Context.ConnectionId); | ||
| return base.OnDisconnected(stopCalled); | ||
| } | ||
|
|
||
| public void SendPath(string path) | ||
| { | ||
| Image.Add(path); | ||
| Images.Add(path); | ||
| Clients.Others.DrawPath(path); | ||
| Clients.All.Action(DateTime.Now.TimeOfDay, ActionType.Drawing.ToString(), Context.ConnectionId); | ||
| } | ||
|
|
||
| public void Clear() | ||
| { | ||
| Image.Clear(); | ||
| Images.Clear(); | ||
| Clients.All.Clear(); | ||
| Clients.All.Action(DateTime.Now.TimeOfDay, ActionType.Clearing.ToString(), Context.ConnectionId); | ||
| } | ||
|
|
||
| private void AddClient(string id, string ip) | ||
| { | ||
| Users.ConnectedClients.Add(id,ip); | ||
| Clients.All.NumberOfUsers(Users.Size()); | ||
| Clients.All.Action(DateTime.Now.TimeOfDay,ActionType.Connecting.ToString(), id); | ||
| Clients.All.Users(Users.GetJson()); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can just pass user collection. Let the signalR to serialize. |
||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This three calls Client.All can be aggregate in one. |
||
| private void RemoveClient(string id) | ||
| { | ||
| Users.ConnectedClients.Remove(id); | ||
| Clients.All.NumberOfUsers(Users.Size()); | ||
| Clients.All.Action(DateTime.Now.TimeOfDay, ActionType.Disconnecting.ToString(), id); | ||
| Clients.All.Users(Users.GetJson()); | ||
| } | ||
| private string GetIpAddress() | ||
| { | ||
| return Context.Request.GetRemoteIpAddress(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -37,5 +68,9 @@ public interface IPunsClientHandler | |
| void DrawPath(string path); | ||
| void Clear(); | ||
| void LoadImage(List<string> image); | ||
| void NumberOfUsers(int volume); | ||
| void Users(string json); | ||
| void Action(TimeSpan data, string action, string user); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| using System.Collections.Generic; | ||
| using Newtonsoft.Json; | ||
|
|
||
| namespace SignalR2.Hubs | ||
| { | ||
| public class UsersHandler | ||
| { | ||
| public Dictionary<string, string> ConnectedClients; | ||
| public UsersHandler() | ||
| { | ||
| ConnectedClients = new Dictionary<string, string>(); | ||
| } | ||
|
|
||
| public int Size() | ||
| { | ||
| return ConnectedClients.Count; | ||
| } | ||
|
|
||
| public string GetJson() | ||
| { | ||
| return JsonConvert.SerializeObject(ConnectedClients); | ||
| } | ||
| } | ||
| public enum ActionType | ||
| { | ||
| Connecting, | ||
| Disconnecting, | ||
| Drawing, | ||
| Clearing, | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
styled! me gusta:)