0

所以我有一个客户端应用程序和一个 WCF 服务发布到 IIS 服务器。我成功地让客户端订阅 WCF 服务,然后在任何一个客户端上单击一个按钮后向客户端应用程序的每个实例推送一条消息。现在我的目标是从 WCF 应用程序中推送消息,但通过 Web 浏览器中的 REST,然后允许我在 Android 中使用此接口并在 Android 客户端和 Windows 客户端应用程序之间进行交互。有人知道我需要做什么吗?这是我工作的界面的基础知识。

IService.vb:

    <ServiceContract(SessionMode:=SessionMode.Required, CallbackContract:=GetType(IServiceCallback))>
Public Interface IService
    <OperationContract(IsOneWay:=True)>
    Sub GetClientCount()

    <OperationContract(IsOneWay:=True)>
    Sub RegisterClient(ByVal id As Guid)

    <OperationContract(IsOneWay:=True)>
    Sub UnRegisterClient(ByVal id As Guid)
End Interface

Public Interface IServiceCallback
    <OperationContract(IsOneWay:=True)>
    Sub SendCount(ByVal count As Int32)
End Interface

服务.svc.vb

<ServiceBehavior(InstanceContextMode:=InstanceContextMode.Single, ConcurrencyMode:=ConcurrencyMode.Multiple)>
Public Class Service
    Implements IService, IRestService
    Private clients As New Dictionary(Of Client, IServiceCallback)()
    Private locker As New Object()

    Public ReadOnly Property Callback As IServiceCallback
        Get
            Return OperationContext.Current.GetCallbackChannel(Of IServiceCallback)()
        End Get
    End Property

    'called by clients to get count
    Public Sub GetClientCount() Implements IService.GetClientCount
        Dim query = ( _
        From c In clients _
        Select c.Value).ToList()

        Dim action As Action(Of IServiceCallback) = Function(Callback) GetCount(Callback)

        query.ForEach(action)
    End Sub

    Private Function GetCount(ByVal callback As IServiceCallback) As Int32
        callback.SendCount(clients.Count)
        Return Nothing
    End Function

    '---add a newly connected client to the dictionary---
    Public Sub RegisterClient(ByVal guid As Guid) Implements IService.RegisterClient
        '---prevent multiple clients adding at the same time---
        SyncLock locker
            clients.Add(New Client With {.id = guid}, Callback)
        End SyncLock
    End Sub

    '---unregister a client by removing its GUID from 
    ' dictionary---
    Public Sub UnRegisterClient(ByVal guid As Guid) Implements IService.UnRegisterClient
        Dim query = From c In clients.Keys _
                    Where c.id = guid _
                    Select c
        clients.Remove(query.First())
    End Sub
End Class
4

2 回答 2

3

通常,在 HTTP 中基于推送的实现非常困难。用于实现这一点的常用技术是反向 AjaxWeb 套接字长轮询(有些甚至使用单像素 flex 对象来保持套接字永久打开)。查看此Code Project Article,它使用 Reverse Ajax aka Commet 来实现能够推送通知的 wcf 休息服务。

于 2012-07-28T07:00:28.403 回答
0

您需要将消息从 Android 设备发送到 wcf 服务,然后 wcf 服务会将消息转发给已注册的 windows 客户端。

于 2016-12-12T16:48:40.250 回答