如何在Red5`fitcDemo`示例中验证用户?

时间:2012-04-19 12:50:46

标签: java chat red5

我正在尝试更改Red5的fitcDemo应用程序的源代码,并在让它连接到服务器之前以某种方式验证用户名,但我不知道如何开始使用!
我的意思是我不知道在客户端连接时将调用哪种方法以及如何从客户端应用程序获取用户名 我会在这里提供源代码,以便有人可以帮助我。

package org.xyz;

import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IClient;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.Red5;
import org.red5.server.api.service.IPendingServiceCall;
import org.red5.server.api.service.IPendingServiceCallback;
import org.red5.server.api.service.IServiceCapableConnection;
import org.red5.server.api.stream.IBroadcastStream;
import org.red5.server.api.stream.IPlayItem;
import org.red5.server.api.stream.IPlaylistSubscriberStream;
import org.red5.server.api.stream.IStreamAwareScopeHandler;
import org.red5.server.api.stream.ISubscriberStream;


public class Application extends ApplicationAdapter
  implements IPendingServiceCallback, IStreamAwareScopeHandler
{
  protected static Log log = LogFactory.getLog(Application.class);

  public boolean appStart(IScope scope)
  {
    return true;
  }

  public boolean appConnect(IConnection conn, Object[] params)
  {
    IServiceCapableConnection service = (IServiceCapableConnection)conn;
    log.info("Client connected {} conn {}");
    service.invoke("setId", new Object[] { conn.getClient().getId() }, this);
    return true;
  }

  public boolean appJoin(IClient client, IScope scope)
  {
    log.info("Client joined app {}");

    IConnection conn = Red5.getConnectionLocal();
    return true;
  }

  public void streamPublishStart(IBroadcastStream stream)
  {
    if (log.isDebugEnabled()) {
      log.debug("stream broadcast start: {}");
    }
    IConnection current = Red5.getConnectionLocal();
    for (Set<IConnection> connections : this.scope.getConnections())
      for (IConnection conn : connections) {
        if (conn.equals(current))
        {
          continue;
        }

        if ((conn instanceof IServiceCapableConnection)) {
          ((IServiceCapableConnection)conn).invoke("newStream", new Object[] { stream.getPublishedName() }, this);

          if (log.isDebugEnabled())
            log.debug("sending notification to {}");
        }
      }
  }

  public void streamRecordStart(IBroadcastStream stream)
  {
  }

  public void streamBroadcastClose(IBroadcastStream stream)
  {
  }

  public void streamBroadcastStart(IBroadcastStream stream)
  {
  }

  public void streamPlaylistItemPlay(IPlaylistSubscriberStream stream, IPlayItem item, boolean isLive)
  {
  }

  public void streamPlaylistItemStop(IPlaylistSubscriberStream stream, IPlayItem item)
  {
  }

  public void streamPlaylistVODItemPause(IPlaylistSubscriberStream stream, IPlayItem item, int position)
  {
  }

  public void streamPlaylistVODItemResume(IPlaylistSubscriberStream stream, IPlayItem item, int position)
  {
  }

  public void streamPlaylistVODItemSeek(IPlaylistSubscriberStream stream, IPlayItem item, int position)
  {
  }

  public void streamSubscriberClose(ISubscriberStream stream)
  {
  }

  public void streamSubscriberStart(ISubscriberStream stream)
  {
  }

  public List<String> getStreams()
  {
    IConnection conn = Red5.getConnectionLocal();
    return getBroadcastStreamNames(conn.getScope());
  }

  public void resultReceived(IPendingServiceCall call)
  {
    log.info("Received result {} for {}");
  }
}

1 个答案:

答案 0 :(得分:1)

虽然Red5提供了这样做的方法: How to Identify Publishers and Consumers using Red5 API 我需要使用数据库来区分它们,所以这是我的方法。 假设您已经在red5中使用与用户相关的表连接数据库。 如果没有,官方教程是一个开始的好地方: http://www.red5tutorials.net/index.php/Tutorials:MySQL_and_Red5 虽然本教程适用于MySQL,但您可以为数据库使用适当的JDBC驱动程序。

首先,您需要使用as3在Flash中的NetConnection的connect()中从客户端发送用户ID:

var nc:NetConnection = new NetConnection();
var userID:String = "foo";
nc.connect("rtmp://" + ipAddrOfRed5Server + "/fitcDemo", userID);

在Red5中:

public boolean appConnect(IConnection conn , Object[] params )
{
    try { 
         String uID = (String)params[0];             
         //Run a query here to check if the user exists in the DB. 
             // If not, you can call rejectClient("Not an existing user!");
             // Once authenticated here, your normal logic follows
    }
    } catch(Exception e){
        e.printStackTrace();
        log.error("Error handling User connect"); 
    }       
    return true;

}