自托管websocket服务多操作合约?
首先,对不起我的英语 我有一个问题,我搜索了一下,但没有找到任何答案 我在上找到了一个应答码 这段代码运行良好 WebSocket服务和服务器:自托管websocket服务多操作合约?,websocket,Websocket,首先,对不起我的英语 我有一个问题,我搜索了一下,但没有找到任何答案 我在上找到了一个应答码 这段代码运行良好 WebSocket服务和服务器: // Self-hosted Server start at http://localhost:8080/hello using System; using System.Collections.Generic; using System.Linq; using System.Net.WebSockets; using System.ServiceMo
// Self-hosted Server start at http://localhost:8080/hello
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
namespace WebSocketsServer
{
class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8080/hello");
// Create the ServiceHost.
using(ServiceHost host = new ServiceHost(typeof(WebSocketsServer), baseAddress))
{
// Enable metadata publishing.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
CustomBinding binding = new CustomBinding();
binding.Elements.Add(new ByteStreamMessageEncodingBindingElement());
HttpTransportBindingElement transport = new HttpTransportBindingElement();
//transport.WebSocketSettings = new WebSocketTransportSettings();
transport.WebSocketSettings.TransportUsage = WebSocketTransportUsage.Always;
transport.WebSocketSettings.CreateNotificationOnConnection = true;
binding.Elements.Add(transport);
host.AddServiceEndpoint(typeof(IWebSocketsServer), binding, "");
host.Open();
Console.WriteLine("The service is ready at {0}", baseAddress);
Console.WriteLine("Press <Enter> to stop the service.");
Console.ReadLine();
// Close the ServiceHost.
host.Close();
}
}
}
[ServiceContract(CallbackContract = typeof(IProgressContext))]
public interface IWebSocketsServer
{
[OperationContract(IsOneWay = true, Action = "*")]
void SendMessageToServer(Message msg);
}
[ServiceContract]
interface IProgressContext
{
[OperationContract(IsOneWay = true, Action = "*")]
void ReportProgress(Message msg);
}
public class WebSocketsServer: IWebSocketsServer
{
public void SendMessageToServer(Message msg)
{
var callback = OperationContext.Current.GetCallbackChannel<IProgressContext>();
if(msg.IsEmpty || ((IChannel)callback).State != CommunicationState.Opened)
{
return;
}
byte[] body = msg.GetBody<byte[]>();
string msgTextFromClient = Encoding.UTF8.GetString(body);
string msgTextToClient = string.Format(
"Got message {0} at {1}",
msgTextFromClient,
DateTime.Now.ToLongTimeString());
callback.ReportProgress(CreateMessage(msgTextToClient));
}
private Message CreateMessage(string msgText)
{
Message msg = ByteStreamMessage.CreateMessage(
new ArraySegment<byte>(Encoding.UTF8.GetBytes(msgText)));
msg.Properties["WebSocketMessageProperty"] =
new WebSocketMessageProperty
{
MessageType = WebSocketMessageType.Text
};
return msg;
}
}
}
当我删除Action=“*”参数时,它不起作用
但是我想添加新的方法,比如SendMessageToServer
[ServiceContract(CallbackContract = typeof(IProgressContext))]
public interface IWebSocketsServer
{
[OperationContract(IsOneWay = true, Action = "*")]
void SendMessageToServer(Message msg);
[OperationContract(IsOneWay = true, Action = "*")]
void DifferentMethod(string msg);
}
但当自托管服务器启动时,此代码抛出错误“ServiceContract具有多个操作,操作为“”。ServiceContract最多只能具有一个操作,操作为”“。”
我试图更改动作参数的值,如“发送”、“测试”。服务器启动时没有问题。
但客户端未连接到“ws://localhost:8080/hello”
我想调用这样的方法
ws = new WebSocket("ws://localhost:8080/Send");
ws = new WebSocket("ws://localhost:8080/Test");
我需要帮助。您不能有两份指向“*”的运营合同。这将生成您收到的错误消息。 见: 指定的两个调用将创建两个WebSocket连接,如果在这些端点上有服务,则每个端点一个连接
ws = new WebSocket("ws://localhost:8080/Send");
ws = new WebSocket("ws://localhost:8080/Test");
我认为您需要的是WebSocket连接,然后使用ws.Send(message)向WebSocket服务器发送消息。如果对消息使用子协议,则可以获得所需的灵活性
希望这能帮助您在WebSocket方面取得进展 谢谢你的回答。我在消息中发送json数据,并在服务中解析。Json数据包含“HandlerType”、“ActionType”。调用类的套接字服务:)
ws = new WebSocket("ws://localhost:8080/Send");
ws = new WebSocket("ws://localhost:8080/Test");
ws = new WebSocket("ws://localhost:8080/Send");
ws = new WebSocket("ws://localhost:8080/Test");