'signal' method

pull/20/head
pabloFuente 2017-12-11 14:50:41 +01:00
parent 1f74a554a4
commit 7f65f8ed5e
9 changed files with 116 additions and 39 deletions

View File

@ -1,6 +1,6 @@
{
"name": "openvidu-browser",
"version": "1.1.2",
"version": "1.1.3",
"description": "OpenVidu Browser",
"author": "OpenVidu",
"license": "Apache-2.0",

View File

@ -1,4 +1,4 @@
import { SessionInternal, SessionOptions } from '../OpenViduInternal/SessionInternal';
import { SessionInternal, SessionOptions, SignalOptions } from '../OpenViduInternal/SessionInternal';
import { Stream } from '../OpenViduInternal/Stream';
import { Connection } from "../OpenViduInternal/Connection";
@ -128,4 +128,23 @@ export class Session {
subscriber.stream.removeVideo();
}
signal(signal: SignalOptions, completionHandler?: Function) {
var signalMessage = {};
if (signal.to && signal.to.length > 0) {
let connectionIds: string[] = [];
for (let i = 0; i < signal.to.length; i++) {
connectionIds.push(signal.to[i].connectionId);
}
signalMessage['to'] = connectionIds;
} else {
signalMessage['to'] = [];
}
signalMessage['data'] = signal.data ? signal.data : '';
signalMessage['type'] = signal.type ? signal.type : '';
this.openVidu.openVidu.sendMessage(this.sessionId, this.connection.connectionId, JSON.stringify(signalMessage));
}
}

View File

@ -14,6 +14,12 @@ export interface SessionOptions {
thresholdSpeaker?: number;
}
export interface SignalOptions {
type?: string;
to?: Connection[];
data?: string;
}
export class SessionInternal {
private id: string;
@ -364,20 +370,20 @@ export class SessionInternal {
onNewMessage(msg) {
console.info("New message: " + JSON.stringify(msg));
let room = msg.room;
let user = msg.user;
let message = msg.message;
console.info("New signal: " + JSON.stringify(msg));
if (user !== undefined) {
this.ee.emitEvent('newMessage', [{
room: room,
user: user,
message: message
this.ee.emitEvent('signal', [{
data: msg.data,
from: this.participants[msg.from],
type: msg.type
}]);
} else {
console.warn("User undefined in new message:", msg);
}
this.ee.emitEvent('signal:' + msg.type, [{
data: msg.data,
from: this.participants[msg.from],
type: msg.type
}]);
}
recvIceCandidate(msg) {

View File

@ -43,7 +43,10 @@ public class OpenViduException extends JsonRpcErrorException {
USER_UNAUTHORIZED_ERROR_CODE(401), ROLE_NOT_FOUND_ERROR_CODE(402),
SESSIONID_CANNOT_BE_CREATED_ERROR_CODE(403), TOKEN_CANNOT_BE_CREATED_ERROR_CODE(404),
USER_METADATA_FORMAT_INVALID_ERROR_CODE(500);
USER_METADATA_FORMAT_INVALID_ERROR_CODE(500),
SIGNAL_FORMAT_INVALID_ERROR_CODE(600), SIGNAL_TO_INVALID_ERROR_CODE(601),
SIGNAL_MESSAGE_INVALID_ERROR_CODE(602);
private int value;

View File

@ -109,13 +109,13 @@ public class ServerJsonRpcHandler extends DefaultJsonRpcHandler<JsonObject> {
private Notification participantSendMessage(Transaction transaction,
Request<JsonObject> request) {
String room = JsonRoomUtils.getRequestParam(request,
ProtocolElements.PARTICIPANTSENDMESSAGE_ROOM_PARAM, String.class);
String user = JsonRoomUtils.getRequestParam(request,
ProtocolElements.PARTICIPANTSENDMESSAGE_USER_PARAM, String.class);
String message = JsonRoomUtils.getRequestParam(request,
ProtocolElements.PARTICIPANTSENDMESSAGE_MESSAGE_PARAM, String.class);
SendMessageInfo eventInfo = new SendMessageInfo(room, user, message);
String data = JsonRoomUtils.getRequestParam(request,
ProtocolElements.PARTICIPANTSENDMESSAGE_DATA_PARAM, String.class);
String from = JsonRoomUtils.getRequestParam(request,
ProtocolElements.PARTICIPANTSENDMESSAGE_FROM_PARAM, String.class);
String type = JsonRoomUtils.getRequestParam(request,
ProtocolElements.PARTICIPANTSENDMESSAGE_TYPE_PARAM, String.class);
SendMessageInfo eventInfo = new SendMessageInfo(data, from, type);
log.debug("Recvd send message event {}", eventInfo);
return eventInfo;
}

View File

@ -93,9 +93,9 @@ public class ProtocolElements {
public static final String PARTICIPANTUNPUBLISHED_NAME_PARAM = "name";
public static final String PARTICIPANTSENDMESSAGE_METHOD = "sendMessage";
public static final String PARTICIPANTSENDMESSAGE_USER_PARAM = "user";
public static final String PARTICIPANTSENDMESSAGE_ROOM_PARAM = "room";
public static final String PARTICIPANTSENDMESSAGE_MESSAGE_PARAM = "message";
public static final String PARTICIPANTSENDMESSAGE_DATA_PARAM = "data";
public static final String PARTICIPANTSENDMESSAGE_FROM_PARAM = "from";
public static final String PARTICIPANTSENDMESSAGE_TYPE_PARAM = "type";
public static final String ROOMCLOSED_METHOD = "roomClosed";
public static final String ROOMCLOSED_ROOM_PARAM = "room";

View File

@ -27,6 +27,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import io.openvidu.client.OpenViduException;
import io.openvidu.client.OpenViduException.Code;
import io.openvidu.server.core.RoomManager.JoinRoomReturnValue;
@ -265,10 +269,17 @@ public class NotificationRoomManager {
throw new OpenViduException(Code.ROOM_NOT_FOUND_ERROR_CODE,
"Provided room name '" + roomName + "' differs from the participant's room");
}
notificationRoomHandler.onSendMessage(request, message, userName, roomName,
internalManager.getParticipants(roomName), null);
try {
JsonObject messageJSON = new JsonParser().parse(message).getAsJsonObject();
notificationRoomHandler.onSendMessage(request, messageJSON, userName, roomName, internalManager.getParticipants(roomName), null);
} catch (JsonSyntaxException | IllegalStateException e) {
throw new OpenViduException(Code.SIGNAL_FORMAT_INVALID_ERROR_CODE,
"Provided signal object '" + message + "' has not a valid JSON format");
}
} catch (OpenViduException e) {
log.warn("PARTICIPANT {}: Error sending message", userName, e);
log.warn("PARTICIPANT {}: Error sending signal", userName, e);
notificationRoomHandler.onSendMessage(request, null, null, null, null, e);
}
}

View File

@ -20,6 +20,8 @@ import java.util.Set;
import org.kurento.client.MediaElement;
import com.google.gson.JsonObject;
import io.openvidu.client.OpenViduException;
import io.openvidu.server.core.NotificationRoomManager;
import io.openvidu.server.core.api.pojo.ParticipantRequest;
@ -190,7 +192,7 @@ public interface NotificationRoomHandler extends RoomHandler {
* null, then the operation was unsuccessful and the user should be responded
* accordingly.
*/
void onSendMessage(ParticipantRequest request, String message, String userName, String roomName,
void onSendMessage(ParticipantRequest request, JsonObject message, String userName, String roomName,
Set<UserParticipant> participants, OpenViduException error);
/**

View File

@ -16,15 +16,20 @@
package io.openvidu.server.core.internal;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.kurento.client.IceCandidate;
import org.springframework.beans.factory.annotation.Autowired;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.openvidu.client.OpenViduException;
import io.openvidu.client.OpenViduException.Code;
import io.openvidu.client.internal.ProtocolElements;
import io.openvidu.server.InfoHandler;
import io.openvidu.server.core.api.NotificationRoomHandler;
@ -200,23 +205,54 @@ public class DefaultNotificationRoomHandler implements NotificationRoomHandler {
}
@Override
public void onSendMessage(ParticipantRequest request, String message, String userName,
public void onSendMessage(ParticipantRequest request, JsonObject message, String userName,
String roomName, Set<UserParticipant> participants, OpenViduException error) {
if (error != null) {
notifService.sendErrorResponse(request, null, error);
return;
}
notifService.sendResponse(request, new JsonObject());
JsonObject params = new JsonObject();
params.addProperty(ProtocolElements.PARTICIPANTSENDMESSAGE_ROOM_PARAM, roomName);
params.addProperty(ProtocolElements.PARTICIPANTSENDMESSAGE_USER_PARAM, userName);
params.addProperty(ProtocolElements.PARTICIPANTSENDMESSAGE_MESSAGE_PARAM, message);
params.addProperty(ProtocolElements.PARTICIPANTSENDMESSAGE_DATA_PARAM, message.get("data").getAsString());
params.addProperty(ProtocolElements.PARTICIPANTSENDMESSAGE_FROM_PARAM, userName);
params.addProperty(ProtocolElements.PARTICIPANTSENDMESSAGE_TYPE_PARAM, message.get("type").getAsString());
Set<String> toSet = new HashSet<String>();
if (message.has("to")) {
JsonArray toJson = message.get("to").getAsJsonArray();
for (int i=0; i < toJson.size(); i++) {
JsonElement el = toJson.get(i);
if (el.isJsonNull()) {
throw new OpenViduException(Code.SIGNAL_TO_INVALID_ERROR_CODE, "Signal \"to\" field invalid format: null");
}
toSet.add(el.getAsString());
}
}
if (toSet.isEmpty()) {
for (UserParticipant participant : participants) {
notifService.sendNotification(participant.getParticipantId(),
ProtocolElements.PARTICIPANTSENDMESSAGE_METHOD, params);
}
} else {
Set<String> participantNames = participants.stream()
.map(UserParticipant::getUserName)
.collect(Collectors.toSet());
for (String to : toSet) {
if (participantNames.contains(to)) {
Optional<UserParticipant> p = participants.stream()
.filter(x -> to.equals(x.getUserName()))
.findFirst();
notifService.sendNotification(p.get().getParticipantId(),
ProtocolElements.PARTICIPANTSENDMESSAGE_METHOD, params);
} else {
throw new OpenViduException(Code.SIGNAL_TO_INVALID_ERROR_CODE, "Signal \"to\" field invalid format: Connection [" + to + "] does not exist");
}
}
}
notifService.sendResponse(request, new JsonObject());
}
@Override