Paul Schneider 8 years ago
commit 7d3f91219e
3 changed files with 58 additions and 42 deletions

@ -19,55 +19,65 @@
// You should have received a copy of the GNU Lesser General Public License // You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Security.Claims;
using System.Security.Principal;
using Microsoft.AspNet.SignalR; using Microsoft.AspNet.SignalR;
namespace Yavsc namespace Yavsc
{ {
public class ChatHub : Hub public class ChatHub : Hub
{
public override Task OnConnected()
{ {
public override Task OnConnected() bool isAuth = false;
{ string userId = null;
if (Context.User!=null) { if (Context.User!=null) {
var group = (Context.User.Identity.IsAuthenticated)? isAuth = Context.User.Identity.IsAuthenticated;
"authenticated":"anonymous"; userId = Context.User.Identity.Name;
// Log ("Cx: " + group); var group = isAuth ?
Groups.Add(Context.ConnectionId, group); "authenticated":"anonymous";
} else Groups.Add(Context.ConnectionId, "anonymous"); // Log ("Cx: " + group);
return base.OnConnected (); Groups.Add(Context.ConnectionId, group);
} } else Groups.Add(Context.ConnectionId, "anonymous");
public override Task OnDisconnected (bool stopCalled)
{ Clients.Group("authenticated").notify("connected", Context.ConnectionId, userId);
return base.OnDisconnected (stopCalled); return base.OnConnected ();
} }
public override Task OnDisconnected (bool stopCalled)
{
string userId = Context.User?.Identity.Name;
Clients.Group("authenticated").notify("disconnected", Context.ConnectionId, userId);
return base.OnDisconnected (stopCalled);
}
public override Task OnReconnected () public override Task OnReconnected ()
{ {
return base.OnReconnected (); return base.OnReconnected ();
} }
public void Send(string name, string message) public void Send(string name, string message)
{ {
Clients.All.addMessage(name,message); Clients.All.addMessage(name,message);
} }
[Authorize] [Authorize]
public void AuthSend (string name, string message) public void AuthSend (string name, string message)
{ {
Clients.All.addMessage("#"+name,message); Clients.All.addMessage("#"+name,message);
} }
[Authorize] [Authorize]
public void PV (string userId, string message) public void PV (string connectionId, string message)
{ {
var sender = Context.User.Identity.Name; var sender = Context.User.Identity.Name;
// TODO personal black|white list + // TODO personal black|white list +
// Contact list allowed only + // Contact list allowed only +
// only pro // only pro
var hubCxContext = Clients.User(userId); var hubCxContext = Clients.User(connectionId);
var cli = Clients.Client(hubCxContext.ConnectionId); var cli = Clients.Client(connectionId);
cli.addPV(sender,message); cli.addPV(sender,message);
} }
} }
} }

@ -19,6 +19,8 @@
</ul> </ul>
<ul id="private"> <ul id="private">
</ul> </ul>
<ul id="chatnotifs">
</ul>
</div> </div>
@section scripts { @section scripts {
<!--Script references. --> <!--Script references. -->
@ -43,6 +45,11 @@
$('#private').append('<li><strong>' + htmlEncode(name) $('#private').append('<li><strong>' + htmlEncode(name)
+ '</strong>: ' + htmlEncode(message) + '</li>'); + '</strong>: ' + htmlEncode(message) + '</li>');
}; };
chat.client.notify = function (tag, message) {
// Add the pv to the page.
$('#chatnotifs').append('<li><i>' + htmlEncode(tag)
+ '</i> ' + htmlEncode(message) + '</li>');
};
// Get the user name and store it to prepend to messages. // Get the user name and store it to prepend to messages.
$('#displayname').val(prompt('Enter your name:', '')); $('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box. // Set initial focus to message input box.

File diff suppressed because one or more lines are too long
Loading…