Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

c# - SignalR with Web Sockets

I am attempting to get websockets working in my dev environment:

  • Visual Studio 2010
  • Windows 7
  • Signal R 0.51
  • Latest Chrome / Firefox

Unfortunately the Javscript client is using long polling. When I force web-sockets on the client side I can't connect at all:

$.connection.hub.start({ transport: ['webSockets'] })

Server code is self-hosted and based on the sample and looks like:

static void Main(string[] args)
{
    string url = "http://localhost:8081/";
    var server = new Server(url);

    // Map the default hub url (/signalr)
    server.MapHubs();

    // Start the server
    server.Start();

    Console.WriteLine("Server running on {0}", url);

    // Keep going until somebody hits 'x'
    while (true)
    {
        ConsoleKeyInfo ki = Console.ReadKey(true);
        if (ki.Key == ConsoleKey.X)
        {
            break;
        }
    }
}

public class MyHub : Hub
{            
    public void Send(string message)
    {
        Clients.addMessage(message);
    }
}

I've searched around and found nothing definitive. Do I need to specify some extra things, use Visual Studio 2012 or will this only work on Windows 8 / IIS 8?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Even on Windows 8 / .NET 4.5 it was not working initially, but with these additional tips I finally got it working.

  • Install websocket support

    -> Turn Windows features on or off
    -> Internet Information Services 
    -> World Wide Web Services 
    -> Application Development Features 
    -> WebSocket Protocol
    
  • in web.config, under appSettings, add this setting:

    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    

SignalR automatically negotiates websockets, it does not have to be specified and nothing special is needed in code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...