I've got the following server code:
var server = new Server("http://localhost:13170/");
server.MapConnection<EchoConnection>("echo");
server.Start();
But when I connect to it from my client code:
var connection = new Connection("http://localhost:13170/echo");
connection
.Start()
.ContinueWith(t =>
{
if (!t.IsFaulted)
connection.Send("Hello");
else
Console.WriteLine(t.Exception);
});
...it reports:
System.Net.WebException: The remote server returned an error: (404) Not Found.
What am I doing wrong?
Turns out that the parameter to MapConnection must start with a slash:
server.MapConnection<EchoConnection>("/echo");
This is because Server.ResolvePath prepends a slash before looking up the URL in the mappings.
Related
I use http.delete for remove my data.
This code in my service [MyService]:
deleteData(id){
let headers = new Headers(),authtoken = localStorage.getItem('token');
headers.append("Authorization", 'Bearer' + authtoken);
headers.append('X-Requested-With', 'XMLHttpRequest')
headers.append('Content-Type', 'application/json;')
return this.http.delete('http://link/'+id, { headers: headers })
.map((resp:Response)=>resp.json())
.catch((error:any) =>{return Observable.throw(error);});
}
This code in my component:
private delete(id):void{
console.log(id); //show`s id
this.myService.deleteData(id)
.subscribe((data) => {console.log(data)});
}
Show`s error "caused by: A network error occurred". My mistake was elsewhere. It is work.
There is nothing wrong in your code what i can see and you are not getting any error from server either. Try to call the same end point from postman and match the headers and check your Internet connection.
Trying to get signalr up and running.
I keep getting 2 errors back from the server:
GET negotitate url returns 500 Internal Server Error
XMLHttpRequest cannot load http://localhost:10772//signalr/negotiate ...
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 500.
Screenshot provided.
Any ideas?
]1
OK, found it out myself. Spelling error.
The name of the hub mentioned was incorrect.
changed:
var proxy = this.connection.createHubProxy('chattAppHub');
to:
var proxy = this.connection.createHubProxy('ChatAppHub');
In the backend:
[HubName("ChatAppHub")]
public class ChatAppHub : Hub ...
inside startup.cs
var hubConfiguration = new HubConfiguration();
hubConfiguration.EnableDetailedErrors = true;
appBuilder.MapSignalR(hubConfiguration);
appBuilder.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
This fixed it for me:
C# Startup.cs tweak:
app.MapSignalR(new HubConfiguration{EnableJSONP = true});
JavaScript tweak:
connection.start({ jsonp: true })
We need JSONP anyways so for us it was a good solution.
I had the same problem, it is all about CORS.
You should add Host URL in CORS config in Sturtup.cs, Have look at:
https://stackoverflow.com/a/59891997/854405
Executing Angular2 http call to the offline server doesn't provide much info in it's "error response" object I'm getting in the Observable's .catch(error) operator or subscription error delegate (they are both share the same info actually). But as you can see on the screen shot of the console there's actual error was displayed by zone.js somehow.
So, how can I get this specific error info (net::ERR_CONNECTION_REFUSED)?
Thanks.
Whenever server do not respond, response.status will be always equal to 0 (zero)
{
type: 3, //ResponseType.Error
status: 0, // problem connecting endpoint
}
Also note, when you are performing CORS request, but origin (url in browser) is not authorized (not in allowed list of host names configured in remote endpoint) the response would be similar to above, with exception to type attribute which will be equal to 4 = ResponseType.Opaque...
This means, connection was made, but for instance, OPTIONS request returned with headers which do not contain origin or HTTPS request was performed from HTTP origin.
You can handle the error messages so they are easier to read. This can definitely be expanded on too:
public Get() {
return this.http.get(this.URL).map(this.extractData)
.catch(this.handleError);
}
public extractData(res: Response) {
let body = res.json();
return body || {};
}
public handleError(error: any) {
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg);
return Observable.throw(errMsg);
}
Check out this part of the docs on error handling.
Without digging in the code, my expectation is that if the server is unreachable, then no response can be returned from the server. Therefore the Response object remains its initialized state.
First off im using the JDare/ClankBundle
I'm following these instruction to call a function from the client side
https://github.com/JDare/ClankBundle/blob/master/Resources/docs/RPCSetup.md
I keep gettting this error on the console "RPC Error [object Object] undefined"
I noticed that it doesn't matter if I mispell the notify_func function, it still throws the same error.
I don't know what im doing wrong, but I assume the function isn't found to begin with?
This is the client side (Twig)
The websocket connects succesfully
<script type="text/javascript">
var myClank = Clank.connect('ws://localhost:9999');
myClank.on("socket/connect", function(session){
alertify.success("Connected to websocket server");
//this will call the server side function "Sample::addFunc"
session.call("notify/notify_func",[2,5])
.then( //using "then" promises.
function(result) //the function for a valid result
{
alertify.log("RPC Valid! "+result);
},
function(error, desc) // the function to handle an error
{
alertify.log("RPC Error"+" "+error+" "+desc);
}
);
});
myClank.on("socket/disconnect", function(error){
alertify.error("Disconnect for "+error.reason+" with code "+error.code);
});
</script>
This is the server side
<?php
namespace Gabriel\NotificationsBundle\RPC;
use Ratchet\ConnectionInterface as Conn;
class PostCommentNotificationService
{
public function notifyFunc(Conn $conn,$params)
{
return array("result"=>array_sum($params));
}
}
This is the config
#services.yml
services:
notifications.newcommentpost:
class: Gabriel\NotificationsBundle\RPC\PostCommentNotificationService
#config.yml
# Clank Configuration
clank:
web_socket_server:
port: 9999 #The port the socket server will listen on
host: localhost #(optional) The host ip to bind to
rpc:
-
name: "notify" #Important! this is the network namespace used to match calls to this service!
service: "notifications.newcommentpost" #The service id.
I had a very similar problem.
I found out that stopping and restarting the built-in websocket server solved the problem. Every modification done on the websocket code requires the websocket server to be restarted in order for these changes to take place...
php app/console clank:server
I have a thread class which makes a web request. After 20-30 urls request, its throwing exception: System.Web.HttpException: Request timed out.
My code is below where it is throwing exception:
httpReq.AllowAutoRedirect = false;
httpReq.KeepAlive = false;
httpReq.Headers.Add("Location", "");
httpReq.Timeout = this.HttpRequestTimeout;
httpRes = (HttpWebResponse)httpReq.GetResponse();
In last line : httpRes = (HttpWebResponse)httpReq.GetResponse();
it is throwing exception.
"The remote server returned an error: (403) Forbidden."
I am using session for setting some values with the request header.
I got the answer. I was not using httpRes.close() method after using response object. That's why after 80-90 request it was returning : "The remote server returned an error: (403) Forbidden ".