简体   繁体   中英

SignalR Server Desktop application with Mosync mobile client application

Now I am developing server desktop application in c#(visual Studio 2012) using SignalR .

Client Application using Mosync Mobile application(Mobile Platform Independent)

When server application and client application is on same machine(localhost), communication is successfully created and data feed from server to client is working fine. But When i put server application in remote server, Mosync client application is not communicate with server. Could any one help me?

Server side code:

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);
    }

}

}

Client mobile application Code(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>

Assuming your code is ok, which it does at first glance, then it is probably either an IIS or network/infrastructure issue. Have you investigated this possibility ?

SignalR is supported on IIS 7.0 and 7.5, but support for extensionless URLs must be added. To add support for extensionless URLs, see http://support.microsoft.com/kb/980368

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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