2

I have SingnalR (OWIN self-hosted) server hub working with .net client. Now i'm preparing to write web client. I see that hub scripts are served http://localhost:10102/signalr/hubs but cannot see Scripts/jquery-.min.js and Scripts/jquery.signalR-.min.js.

I assume those scripts are not served from server hub (but by default included by nuget to solution) - am i right or missing something? Is there a way to reference those scripts directly form server (not to copy and host them on javascript client side)?

3 Answers 3

2

General:

https://learn.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client:

A JavaScript client requires references to jQuery and the SignalR core JavaScript file. The jQuery version must be 1.6.4 or major later versions, such as 1.7.2, 1.8.2, or 1.9.1. If you decide to use the generated proxy, you also need a reference to the SignalR generated proxy JavaScript file. The following example shows what the references might look like in an HTML page that uses the generated proxy.

You have only to add the following scripts to your index.html (take care about the versions):

<script src="Scripts/jquery-1.10.2.min.js"></script>
<script src="Scripts/jquery.signalR-2.1.0.min.js"></script>
<script src="signalr/hubs"></script>

Serve these files from server:

  1. Create directory in server project where you place these JS Files
  2. Configure your server that he serves theses files. For that add app.UseFileServer(); to you Configure(...) method in Startup class. (See details about service files: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files)
  3. Add the required scripts in client. There is an example (change adresses and script file to your files and you server adress:

    <script type="text/javascript" src="http://localhost:10310/scripts/signalr-clientES5-1.0.0-alpha2-final.js></script>

Sign up to request clarification or add additional context in comments.

5 Comments

Scripts/jquery-1.10.2.min.js is part of client side. Can it be served from signalr server host?
@ITMan: I edited my anwer. This solutions works. I tested it for short.
Got it work with inspiration of your answer. Thanks for your help! I'm not sure how to mark the answer inside this thread...?
@IT Man. Check mark on left side? meta.stackexchange.com/questions/147531/…
:) well i know how to mark the answer. I was not sure if your answer i exact one - for sure it was right clue!
0

Actually, you don't need to have scripts to implement SygnalR service (backend part). To make a connection between client index.html and your service, you need to have some kind of client lib that works with SygnalR to establish a connection.

1 Comment

I know i can include those scripts from anywhere but i would rather like to serve them from signalr server host. That would ensure version compatibility for example.
0

Here's the EXACT solution I came into, based on answers inside this thread:

Static files (like additional javascript files) can be served within same host with configuration of below. Scripts will be available at http://{yourhost}/scripts/{scriptName} when placed inside \Scripts folder iniside solution ('copy if newer' has to be set for 'Copy To Output Directory' for each of the files).

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Branch the pipeline here for requests that start with "/signalr"
            app.Map("/signalr", map =>
            {
                // Setup the CORS middleware to run before SignalR.
                // By default this will allow all origins. You can 
                // configure the set of origins and/or http verbs by
                // providing a cors options with a different policy.
                map.UseCors(CorsOptions.AllowAll);
                var hubConfiguration = new HubConfiguration
                {
                    // You can enable JSONP by uncommenting line below.
                    // JSONP requests are insecure but some older browsers (and some
                    // versions of IE) require JSONP to work cross domain
                    // EnableJSONP = true
                };
                // Run the SignalR pipeline. We're not using MapSignalR
                // since this branch already runs under the "/signalr"
                // path.
                map.RunSignalR(hubConfiguration);
            });

            // Serving javascript libraries required for client as static content from Scripts folder
            app.UseStaticFiles(new StaticFileOptions() {
                RequestPath = new PathString("/scripts"),
                FileSystem = new PhysicalFileSystem(@".\Scripts"),
            });
        }
    }

IT Man

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.