繁体   English   中英

具有Mosync移动客户端应用程序的SignalR Server桌面应用程序

[英]SignalR Server Desktop application with Mosync mobile client application

现在,我正在使用SignalR在c#(visual Studio 2012)中开发服务器桌面应用程序。

使用Mosync Mobile应用程序的客户端应用程序(独立于移动平台)

当服务器应用程序和客户端应用程序位于同一台计算机(本地主机)上时,通信已成功创建,并且从服务器到客户端的数据馈送正常运行。 但是,当我将服务器应用程序放在远程服务器中时,Mosync客户端应用程序无法与服务器通信。 有人可以帮我吗?

服务器端代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Reflection;

using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Microsoft.Owin.Cors;
using Microsoft.Owin;
using Owin;

namespace SampleSignalRServer
{
    public partial class Form1 : Form
    {
    private IDisposable signalR { get; set; }
    const string ServerURI = "http://localhost:8080";
    MyHub h = new MyHub();
    public Form1()
    {
        InitializeComponent();
    }

    private void btnServerStart_Click(object sender, EventArgs e)
    {
        writeToConsole("Starting server...");
        btnServerStart.Enabled = false;
        Task.Run(() => StartServer());
    }
    private void StartServer()
    {
        try
        {
            signalR = WebApp.Start(ServerURI);

        }
        catch (TargetInvocationException)
        {
            writeToConsole("Server failed to start. A server is already running on" + ServerURI);
            this.Invoke((Action)(() => btnServerStart.Enabled = true));
            return;
        }
        this.Invoke((Action)(() => btnServerStart.Enabled = true));
        writeToConsole("Server started at" + ServerURI);
    }
    public void writeToConsole(string message)
    {
        if (RichTextBoxConsole.InvokeRequired)
        {
            this.Invoke((Action)(() => writeToConsole(message)));
            return;
        }
        RichTextBoxConsole.AppendText(message + Environment.NewLine);
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (signalR != null)
        {
            signalR.Dispose();
        }
    }
    private void btnSend_Click(object sender, EventArgs e)
    {
        string msg = txtMessage.Text;
        h.Receive(msg);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        string message = "hi";
       // h.Receive(message);
    }


}
class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseCors(CorsOptions.AllowAll);
        app.MapSignalR();
    }
}
public class MyHub : Hub
{
    public void Send(string name, string message)
    {
        Clients.All.addMessage(name, message);
        Program.mainform.writeToConsole(name + " : " + message);
    }
    public void Receive(string msg)
    {
        var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
        context.Clients.All.addMessage("Admin", msg);
    }
    public override Task OnConnected()
    {
        Program.mainform.writeToConsole("Client Connected:" + Context.ConnectionId);

        return base.OnConnected();
    }
    public override Task OnDisconnected(bool stopCalled)
    {
        Program.mainform.writeToConsole("Client DisConnected: " + Context.ConnectionId);
        return base.OnDisconnected(stopCalled);
    }

}

}

客户端移动应用程序代码(MOsync- HTML + Javascript)

    <!DOCTYPE html>
<!--
* @file index.html
*
* Template application that shows examples of how to access
* device services from JavaScript using the Wormhole library.
-->
<html>
    <head>
    <title>SignalR Simple Chat</title>
    <style type="text/css">
        .container {
            background-color: #99CCFF;
            border: thick solid #808080;
            padding: 20px;
            margin: 20px;
        }
    </style>
        <meta name="viewport" content="width=320, user-scalable=no">
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
        <title>Wormhole Template App</title>
        <link rel="stylesheet" href="style.css" type="text/css" media="screen" title="no title" charset="utf-8">
        <script type="text/javascript" charset="utf-8" src="js/wormhole.js"></script>
        <script src="js/jquery-1.6.4.min.js"></script>
        <script src="js/jquery.signalR-2.0.3.min.js"></script>
        <script src="http://localhost:8080/signalr/hubs"></script>
        <script type="text/javascript">

            function StartConnection()
            {
                alert("Start Button Clicked");
                  $.connection.hub.url = "http://localhost:8080/signalr";

                    var chats = $.connection.myHub;
                    alert(chats);

                    chats.client.addMessage = function (name, message) 
                    {                   
                        var encodedName = $('<div />').text(name).html();
                        var encodedMsg = $('<div />').text(message).html();
                        // Add the message to the page.
                        $('#discussion').append('<li><strong>' + encodedName
                            + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
                    };

                    // Get the user name and store it to prepend to messages.
                    $('#displayname').val(prompt('Enter your name:', ''));
                    // Set initial focus to message input box.
                    $('#message').focus();


                    // Start the connection.
                    $.connection.hub.start().done(function () {
                        $('#sendmessage').click(function () {                          
                            chats.server.send($('#displayname').val(), $('#message').val());                            
                            $('#message').val('').focus();

                        });
                    });
            }

            // Register event listeners.

            // The "deviceready" event is sent when the system
            // has finished loading.
            document.addEventListener(
                "deviceready",
                displayDeviceInfo,
                true);

            // Close the application when the back key is pressed.
            document.addEventListener(
                "backbutton",
                function() { mosync.app.exit(); },
                true);
        </script>
    </head>
    <body>
        <div class="container">
        <input type="text" id="message" />
        <input type="button" id="sendmessage" value="Send" />
        <input type="button" id="sendmessagfe" value="localhost:8080" />
        <input type="hidden" id="displayname" />
        <input type="button" value="Start" onclick="StartConnection()"/>
        <ul id="discussion"></ul>

        </div>
    </body>
</html>

乍一看,假设您的代码可以,那么它可能是IIS或网络/基础结构问题。 您是否调查过这种可能性

IIS 7.0和7.5支持SignalR,但必须添加对无扩展名URL的支持。 要添加对无扩展名URL的支持,请参阅http://support.microsoft.com/kb/980368

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM