现在我正在使用SignalR在c#(visual Studio 2012)中开发服务器桌面应用程序。
使用Mosync移动应用程序(独立于移动平台)的客户端应用程序
当服务器应用程序和客户端应用程序位于同一台计算机(localhost)上时,会成功创建通信,并且从服务器到客户端的数据馈送工作正常。 但当我将服务器应用程序放在远程服务器中时,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>: ' + 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>
答案 0 :(得分:1)
假设您的代码是正常的,乍一看它,那么它可能是IIS或网络/基础设施问题。你有没有调查过this possibility?
IIS 7.0和7.5支持SignalR,但支持无扩展 必须添加网址。要添加对无扩展名网址的支持,请参阅 http://support.microsoft.com/kb/980368