I am trying to connect SignalR hub to a Vue component but I fail doing that. i googled "vue with signalr" and real almost every link up to second page.
I getting a cors origin, but I dont think that this is the main problem, since my post/get call to web api are working well.
c# port number 63213 , client at 8080
I also using vuex and i am wonder if I should connect in at the store.
here are code examples. I use vue/vuex with typescript falvor.
mounted: function() {
//... under mounted, signalR connection. i am using import * as signalR from "#aspnet/signalr";
this.hubConnection = new signalR.HubConnectionBuilder()
.withUrl("http://localhost:63213/ChatHub")
.build();
// connecting to the hub
this.hubConnection
.start()
.then(() => console.log("connection started"))
.catch(err => console.log("connecting hub failed err is : ", err));
//at the hub there is a function named broadcastMessage, should return string that will be added to an array. should it be at sotr's getter
this.connection.on("broadcastMessage", function(msg: string) {
this.messages.push({ msg });
});
},
c#
public class Startup
{
public void Configuration(IAppBuilder app)
{
var policy = new CorsPolicy()
{
AllowAnyOrigin = true,
AllowAnyHeader = true,
AllowAnyMethod = true,
SupportsCredentials = true
};
policy.Origins.Add("http://localhost:8080");
// Any connection or hub wire up and configuration should go here
app.MapSignalR();
}
}
pot get to web api are working well.
hub
public class ChatHub : Hub
{
public static void SendMessage(string msg)
{
var hubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
hubContext.Clients.All.broadcastMessage(msg, " !! !! ");
}
}
error is:
Access to XMLHttpRequest at 'http://localhost:63213/ChatHub/negotiate' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
should i pass the hub connention to the store?
what am i doing wrong?
thank you.
switched to .core object.
under "Configure"
app.UseCors(builder => builder.WithOrigins("http://localhost:8080").AllowAnyMethod().AllowAnyHeader().AllowCredentials());
app.UseSignalR(route => {route.MapHub<UserHub>("/user-hub");} );
under
ConfigureServices
services.AddSignalR();
services.AddCors();
at vue component (ts)
created: function() {
this.$userHub.$on("user-added-event", this.userAddedEvent);
},
beforeDestroy: function() {
//clean SignalR event
this.$userHub.$off("user-added-event", this.userAddedEvent);
},
user-hub.js used to handle connection.
imported as vue plugin
import { HubConnectionBuilder, LogLevel } from "#aspnet/signalr";
export default {
install(Vue) {
const connection = new HubConnectionBuilder()
.withUrl(`${Vue.prototype.$http.defaults.baseURL}/user-hub`)
.configureLogging(LogLevel.Information)
.build();
const userHub = new Vue();
Vue.prototype.$userHub = userHub;
connection.on("AddUserEvent", (userId, userName) => {
userHub.$emit("user-added-event", { userId, userName });
});
// if connection closed, reopen it
let startedPromise = null;
function start() {
startedPromise = connection.start().catch(err => {
return new Promise((resolve, reject) =>
setTimeout(
() =>
start()
.then(resolve)
.catch(reject),
5000
)
);
});
return startedPromise;
}
connection.onclose(() => start());
start();
}
};
full project will be uploaded to git.
Related
I have a Nest-Service with the following main.ts:
async function bootstrap() {
if (!!environment.production) {
const app = await NestFactory.create(AppModule, {
httpsOptions: {
key: fs.readFileSync(environment.ssl.SSL_KEY_PATH),
cert: fs.readFileSync(environment.ssl.SSL_CERT_PATH)
},
});
app.useWebSocketAdapter(new WsAdapter(app));
app.enableCors();
await app.listen(3077);
} else {
const app = await NestFactory.create(AppModule);
app.useWebSocketAdapter(new WsAdapter(app));
app.enableCors();
await app.listen(3077);
}
}
bootstrap();
And two Gateways within the Service:
#WebSocketGateway(3078)
export class ItemsGateway implements OnGatewayConnection, OnGatewayDisconnect { ... }
#WebSocketGateway(3079)
export class UnitsGateway implements OnGatewayConnection, OnGatewayDisconnect { ... }
Without SSL this is working, but when I use the prod mode I canĀ“t establish a secure connection to domain.tld:3078 and :3079.
How can I get the service to listen on all 3 Ports? I think there is the problem, because certs are only attached to the Server listening on Port: 3077, where all my REST-API stuff goes.
Thx, Dom
Edit: This also worked as there was just on WebsocketServer on the same port as the API -> 3077.
Edit 2:
I also tried this, but then comes the error that address is in use on the second attempt to create() a server:
async function bootstrap() {
if (!!environment.production) {
const httpsOptions = {
key: fs.readFileSync(environment.ssl.SSL_KEY_PATH),
cert: fs.readFileSync(environment.ssl.SSL_CERT_PATH)
};
const server = express();
const app = await NestFactory.create(
AppModule,
new ExpressAdapter(server)
);
app.useWebSocketAdapter(new WsAdapter(app));
app.enableCors();
await app.init();
https.createServer(httpsOptions, server).listen(environment.app.port);
https.createServer(httpsOptions, server).listen(environment.websocketPorts.units);
https.createServer(httpsOptions, server).listen(environment.websocketPorts.items);
} else {
const app = await NestFactory.create(AppModule);
app.useWebSocketAdapter(new WsAdapter(app));
app.enableCors();
await app.listen(environment.app.port);
}
}
bootstrap();
You need to .create() a separate app for each port on which you listen for wss connections.
I'm working on some tests using Detox for my React-Native application, one of those test is a flow where I need to check that the user's session is secured. If not, I'm sending an SMS Verification Code.
Test : Success to mock the POST API Call api/sessions/:sessionId, {code : 123456}
Problem : Mirage is not catching the call, so of course my Saga return an error for the fake code 123456, where I want instead Mirage.JS to return true to continue the flow.
Here are the file (file.spec.js):
import { Server } from "miragejs"
import { makeServer } from "./server";
let server;
beforeEach(() => {
server = makeServer({ environment: "development" });
})
afterEach(() => {
server.shutdown()
})
describe('SecureFlow', () => {
it("should do nav to a project and start Investment Flow", async () => {
server.get("https://random-api.eu/sessions/:sessionId", () => {
return new Response( 200, {}, { ok: true });
});
await basicNavigation(); //randomNavigation until the secure part (Screen)
await element(by.id('Accept-andLend')).tap();
await element(by.id('textInput-SMSCode')).typeText("123456");
})
})
server.js
import { Server, Model, Factory } from "miragejs";
export function makeServer({ environment = "development" } = {}) {
let server = new Server({
environment,
models: {
},
routes() {
this.post("https://random-api.eu/sessions/:sessionId", schema => {
return [{ok: true}];
});
}
});
return server;
}
I am building a mobile app in React Native for a backend written in .NET. The backend has implemented a realtime messaging service using SignalR Hubs. I am using the package react-native-signalr. The connection is getting established, and I can send message to SignalR hub by calling proxy.invoke. The problem is in reception of message. I tried with proxy.on but nothing happens.
componentDidMount(){
const { access_token } = this.props;
// Setup connection to signalr hub.
const hubUrl = `${API_URL}/signalr`;
const connection = signalr.hubConnection(hubUrl);
const proxy = connection.createHubProxy('MessagesHub', {queryParams: { token: access_token }});
// Start connection
connection.start();
// Trying to receive message from SignalR Hub
proxy.on('messageReceived', message => {
console.log(message);
})
proxy.on('sendPrivateMessage', message => {
console.log(message);
})
proxy.on('sentPrivateMessage', message => {
console.log(message);
})
}
when you register a listener with your_proxy.on function ,you must define a function with required parameters and bind it on constructor and then pass it to your_proxy.on function ,see bellow:
constructor(props){
super(props);
this.messageReceived=this.messageReceived.bind(this); // <========== **Important**
this.sendPrivateMessage=this.sendPrivateMessage.bind(this); // <========== **Important**
this.sentPrivateMessage=this.sentPrivateMessage.bind(this); // <========== **Important**
}
messageReceived(message ){
console.log(message);
}
sendPrivateMessage(message ){
console.log(message);
}
sentPrivateMessage(message ){
console.log(message);
}
componentDidMount(){
const { access_token } = this.props;
// Setup connection to signalr hub.
const hubUrl = `${API_URL}/signalr`;
const connection = signalr.hubConnection(hubUrl);
const proxy = connection.createHubProxy('MessagesHub', {queryParams: { token: access_token }});
// Trying to receive message from SignalR Hub
proxy.on('messageReceived', this.messageReceived);
proxy.on('sendPrivateMessage', this.sendPrivateMessage);
proxy.on('sentPrivateMessage', this.sentPrivateMessage);
// Start connection
connection.start();
}
I am using SignalR in my angular5 website for presenting lock for documents. I am storing hub connection in data service and soon ad user logged in, I will start the connection. In each component which I need hub connection, I have an Observable and after the connection established, It listens to the hub. Everything is working properly. But after refreshing page, it will not show the result of SignalR command. If I click or mouse over on that page, then it will show the result of SignalR!
This is the code which run after login:
startConnection(): void {
//Create the hub connection for SignalR
this.dataService.connection = $.hubConnection(this.dataService.getServerConn());
this.dataService.authProxy = this.dataService.connection.createHubProxy('auth');
this.dataService.authProxy.on('handler', () => { });
this.dataService.authProxyCreated = false;
this.dataService.connection.qs = { "AuthenticationToken": sessionStorage.getItem('UMToken') };
if (this.dataService.connection.state != $.signalR.connectionState.connected)
this.dataService.connection.start().done(() => {
console.log('Connected to SignalR hub!');
}).catch((error: any) => {
console.log('Hub error -> ' + error);
});
}
and this is the code in component which listen to the hub:
ngOnInit() {
//SignalR
if (this.storeDataService.connection.state === $.signalR.connectionState.connected)
this.registerSignalR();
this.storeDataService.connection.stateChanged((change) => {
if (change.newState === $.signalR.connectionState.connected)
this.registerSignalR();
});
}
ngOnDestroy() {
this.storeDataService.authProxy.off('lockAuth');
this.storeDataService.authProxy.off('unlockAuth');
}
registerSignalR() {
this.storeDataService.authProxy.on('lockAuth', (authNo: string, username: string) => {
var auth = this.queueList.data.find(p => p.AuthNo == authNo);
if (auth) {
auth.LockedOn = new Date();
auth.LockedByUserName = username;
}
});
this.storeDataService.authProxy.on('unlockAuth', (authNo: string) => {
var auth = this.queueList.data.find(p => p.AuthNo == authNo);
if (auth) {
auth.RmiLockedOn = null;
}
});
}
This is also the code in edit page which invoke lock:
if (this.dataService.connection.state === $.signalR.connectionState.connected) {
this.dataService.authProxy.invoke('lock', this.authNo, this.userService.userName, this.userService.userId);
}
this.dataService.connection.stateChanged((change) => {
if (change.newState === $.signalR.connectionState.connected) {
this.dataService.authProxy.invoke('lock', this.authNo, this.userService.userName, this.userService.userId);
}
});
The SignalR Core is generating Hub proxies script, but not adding the "client" methods. (No errors in server or client - only not working)
Generated JS from <script src="http://localhost/signalr/hubs">
proxies['messageHub'] = this.createHubProxy('messageHub');
proxies['messageHub'].client = { };
proxies['messageHub'].server = {
handleMessage: function (receivedString) {
return proxies['messageHub'].invoke.apply(proxies['messageHub'], $.merge(["HandleMessage"], $.makeArray(arguments)));
}
};
Here's the Hub in Server Side:
public class MessageHub : Hub
{
public void HandleMessage(string receivedString)
{
var responseString = string.Empty;
MessageHandler.HandleMessage(receivedString, ref responseString);
Clients.All.sendMessage(responseString);
}
}
The sendMessage methos should be included in the messageHub client proxies in the JS file.
$.connection.messageHub.client.sendMessage is undefined
Only the handleMessage for server proxies was created (and working).
Here's my StartUp.cs inclusions for SignalR:
ConfigureServices:
services.AddMvc(options =>
{
options.Filters.Add(new RoleFilterAttribute());
}).AddJsonOptions(options => options.SerializerSettings.ContractResolver =
new DefaultContractResolver());
services.AddSignalR(options => options.Hubs.EnableDetailedErrors = true)
Configure:
app.UseWebSockets();
app.UseSignalR();
project.json:
"Microsoft.AspNetCore.Mvc": "1.0.0-*",
"Microsoft.AspNetCore.WebSockets": "1.0.0",
"Microsoft.AspNetCore.SignalR.Server": "0.2.0-*",
SOME ADDITIONAL TENTATIVES:
1 - Change method case in Server Side to see if it's mapped:
Clients.All.SendMessage(responseString);
Did not work!
2 - Change the client side to dynamic mapping:
var connection = $.hubConnection('http://localhost/');
var proxy = connection.createHubProxy('messageHub');
connection.start({ withCredentials: false }).done(function () { console.log("CONNECTED") });
proxy.on("sendMessage", function (result) {console.log(result);});
proxy.invoke("handleMessage", msg).done(function(result)console.log(result);});
Again only the handleMessage (server) worked.
Well according to the docs you are missing method name so the send all line should look like this
public void HandleMessage(string receivedString)
{
var responseString = string.Empty;
MessageHandler.HandleMessage(receivedString, ref responseString);
Clients.All.SendMessage("SendMessage",responseString);
}
Also in the following is the correct way
app.UseSignalR(routes =>
{
routes.Hub<MessageHub>("/messageHub");
});
and finally
var connection = $.hubConnection('http://localhost/');
var proxy = connection.createHubProxy('messageHub');
connection.start({ withCredentials: false }).done(function () { console.log("CONNECTED") });
proxy.on("SendMessage", function (result) {console.log(result);});
proxy.invoke("HandleMessage", msg).done(function(result)console.log(result);});
ASP.NET Core SignalR doesn't generate client proxies. There's good advice in the comment to follow the tutorial https://learn.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-6.0&tabs=visual-studio