我正在使用SwiftR与IOS客户端进行SignalR(v2.2.0)聊天。一切正常,IOS客户端以及Web客户端(Asp.net)可以使用以下方式进行聊天:
客户端:
var chatHub = $.connection.chatHub;
$("#btnStartChat").click(function () {
var name = $("#txtNickName").val();//UserName
var userId = $("#txtUserId").val();
var room = $("#txtRoomId").val();
if (name.length > 0) {
chatHub.server.joinRoom(userId, room, name).done(function () {
chatHub.server.connect(userId, room, name);
});
}
else {
alert("Please enter name");
}
});
chatHub.client.messageReceived = function (userName, message) {
AddMessage(userName, message);
$("#istyping").html('');
};
function AddMessage(userName, message) {
$('#divChatWindow').append('<div class="message"><span class="userName">' + userName + '</span>: ' + message + '</div>');
var height = $('#divChatWindow')[0].scrollHeight;
$('#divChatWindow').scrollTop(height);
}
// On New User Connected
chatHub.client.onNewUserConnected = function (id, name) {
// alert('first time add');
AddUser(chatHub, id, name);
};
服务器
public async Task JoinRoom(string user_Id, string room_Id, string user_Name)
{
//Add user in database for room
AddLoginUser(room_Id, this.Context.ConnectionId, user_Id);
await this.Groups.Add(this.Context.ConnectionId, room_Id);
}
public void Connect(string user_Id, string room_Id, string user_Name)
{
var id = Context.ConnectionId;
Clients.Caller.onConnected(id, user_Name, GetRoomUser(room_Id), GetRoomMessage(room_Id));
// send to all in group to update user list
Clients.OthersInGroup(room_Id).onNewUserConnected(id, user_Name);
}
public void SendMessageToAll(string user_Name, string message, string room_Id, string user_Id)
{
// store messages in db
// SaveRoomMessage(user_Id, message, room_Id);
// Broad cast message
Clients.All.messageReceived(user_Name, message);
//Clients.Group(room_Id).messageReceived(user_Name, message);
}
问题:
在上面的方法中如果要改变
Clients.All.messageReceived(user_Name, message);
到
Clients.Group(room_Id).messageReceived(user_Name, message);
即使IOS客户端能够向Web客户端发送消息,IOS客户端也无法接收消息。在这里,Web客户端可以毫无问题地在彼此之间发送和接收消息。
我在网上对此进行了大量搜索,但没有找到任何正确的答案。如果仍然需要更多信息,请告诉我,我会尽量提供。
提前致谢。
答案 0 :(得分:0)
您是否正在做类似的事情来将您的客户添加到您的群组中?
服务器:
attributeText
客户:
<强> JS 强>:
UITextView
swift :
public async Task JoinRoom(string room_id)
{
await this.Groups.Add(this.Context.ConnectionId, room_id);
}
public async Task UnJoinRoom(string room_id)
{
await this.Groups.Remove(this.Context.ConnectionId, room_id);
}
如果你打电话
var chatHub = $.connection.chatHub;
chatHub.server.joinRoom('roomId')
它应该有用。