[英]AbpSession always return null when start using Websocket
I'm using asp.net boilerplate framework, and I'm trying to set the WebSocket connection to become match the logged-in user id when the user establishes the WebSocket connection, but what I noticed is the AbpSession.UserId value is always null, so what is the reason, although in other classes it is working fine, and here is my code: I'm using asp.net boilerplate framework, and I'm trying to set the WebSocket connection to become match the logged-in user id when the user establishes the WebSocket connection, but what I noticed is the AbpSession.UserId value is always null, so这是什么原因,虽然在其他类中它工作正常,这是我的代码:
public class WebSocketServerConnectionManager : ITransientDependency
{
private ConcurrentDictionary<string, WebSocket> _socket = new ConcurrentDictionary<string, WebSocket>();
public IAbpSession AbpSession { get; set; }
public WebSocketServerConnectionManager()
{
AbpSession = NullAbpSession.Instance;
}
public ConcurrentDictionary<string, WebSocket> GetAllSockets()
{
return _socket;
}
public string AddSocket(WebSocket socket)
{
try
{
var ConnId = AbpSession.UserId.ToString(); //<==== Null
// Guid.NewGuid().ToString();
//var test = AbpSession.UserId;
_socket.TryAdd(ConnId/*dictionary item key*/, socket/*dictionary item value*/);
Debug.WriteLine("Connection Added:" + ConnId);
return ConnId;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
}
Updated:更新:
And here is the client-side WebSocket initialization:这是客户端 WebSocket 初始化:
async ngOnInit() {
this.connectWebSocket();
}
connectWebSocket(){
this.socket=new WebSocket(this.webSocketUrl);
this.socket.onopen=(event)=>{
}
this.socket.onclose=(event)=>{
}
this.socket.onmessage=(event)=>{
this.isConnID(event.data);
}
}
You need to pass authentication information.您需要传递身份验证信息。
If you're using the ASP.NET Boilerplate Angular template (with Module Zero), you may pass enc_auth_token
in the query string, which will be resolved by QueryStringTokenResolver
.如果您使用的是 ASP.NET 样板 Angular 模板(带有模块零),您可以在查询字符串中传递enc_auth_token
,这将由QueryStringTokenResolver
解析。
// import { AppConsts } from '@shared/AppConsts';
// import { UtilsService } from 'abp-ng2-module';
connectWebSocket(){
// this.socket=new WebSocket(this.webSocketUrl); // Change this to the following
const encryptedAuthToken = new UtilsService().getCookieValue(AppConsts.authorization.encryptedAuthTokenName);
const webSocketUrl = this.webSocketUrl + '?enc_auth_token=' + encodeURIComponent(encryptedAuthToken);
this.socket = new WebSocket(webSocketUrl);
...
}
Reference: SignalRAspNetCoreHelper.ts参考: SignalRAspNetCoreHelper.ts
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.