问题:如何管理匿名用户,以便在Hub发出响应时,单个浏览器中的多个选项卡都会更新?
方案如下:
我想将SignalR集成到一个项目中,以便匿名用户可以与运营商进行实时聊天.显然,使用iIdentity进行身份验证的用户通过Client.User(username)命令进行映射.但目前一位匿名用户正在浏览site.com/tools和site.com/notTools我无法仅使用connectionID向所有标签发送消息.只有一个选项卡收集响应.
我尝试过使用IWC补丁,但该工具不考虑将聊天信息保存到数据库中,我认为通过ajax传递变量并不是一种读取/写入数据库的安全方法.
我还查看了以下内容:管理匿名用户的SignalR连接
然而,创建一个这样的基础并使用会话似乎不如Owin安全.
我有想法通过每次用户连接时创建一个虚假帐户来使用client.user(),并在断开连接时将其删除.但我宁愿不用垃圾帐户填充我的aspnetusers数据库上下文似乎没必要.
是否可以使用UserHandler.ConnectedIds.Add(Context.ConnectionId);也映射假用户名?我不知所措.
使用iUserId提供商是否有意义?
public interface IUserIdProvider
{
string GetUserId(IRequest request);
}
Run Code Online (Sandbox Code Playgroud)
然后创建一个数据库,将IP地址存储到connectionID和单个用户名?
数据库:
用户:使用FK连接ID
|userid|username|IP |connectionIDs|
| 1 | user1 |127.0.0.1| 1 |
| 2 | user2 |127.0.0.2| 2 |
Run Code Online (Sandbox Code Playgroud)
connectionIDs:
|connectionID|SignalRID|connectionIDs|
| 1 | xx.x.x.x| 1 |
| 2 | xx.xxx.x| 2 |
| 3 | xx.xxxxx| 2 |
| 4 | xxxxx.xx| 2 |
Run Code Online (Sandbox Code Playgroud)
然后可能在连接周围编写逻辑?
public override Task OnConnected()
{
if(Context.Identity.UserName != null){
//add a connection based on the …Run Code Online (Sandbox Code Playgroud) 如何在不使用Switch或If的情况下执行逻辑?
比如check_id_switch($ id)
function check_id_switch($id){
switch($id){
case '1':
$HW = 'Hello, World!';
break;
default:
$HW = 'Goodbye, World!';
break;
}
return $HW;
}
Run Code Online (Sandbox Code Playgroud)
或者实例check_id_if($ id)
function check_id_if($id){
if($id == 1){
$HW = 'Hello, World!';
}
else{
$HW = 'Goodbye, World!';
}
return $HW;
}
Run Code Online (Sandbox Code Playgroud)
check_id_switch($ id)和check_id_if($ id)这两个函数都将检查ID的引用.
如何在不使用php中的if/switch语句的情况下创建与上面相同的逻辑?我也想避免使用forloops.
关于开关/ if的性能有多个争论,但是如果有另一个控制结构,它是在下面还是外面执行上述控制结构?
添加登录脚本作为if语句的示例.我删除了登录脚本的主干.如果为true,则无需查看已完成的操作:false.我觉得下面是笨重而且不洁净的.
if(!empty($_POST))
{
$errors = array();
$username = trim($_POST["username"]);
$password = trim($_POST["password"]);
$remember_choice = trim($_POST["remember_me"]);
if($username == "")
{
$errors[] = "";
}
if($password == "")
{
$errors[] = ""; …Run Code Online (Sandbox Code Playgroud)