|
| 1 | +package org.codeoverflow.chatoverflow.requirement.service.tipeeestream |
| 2 | + |
| 3 | +import java.util.Calendar |
| 4 | + |
| 5 | +import io.socket.client.Socket |
| 6 | +import org.codeoverflow.chatoverflow.WithLogger |
| 7 | +import org.codeoverflow.chatoverflow.connector.Connector |
| 8 | +import org.json.JSONObject |
| 9 | + |
| 10 | +/** |
| 11 | + * The tipeeestream connector connects to the socket.io service to work with incoming events. |
| 12 | + * |
| 13 | + * @param sourceIdentifier the name of the tipeeestream account |
| 14 | + */ |
| 15 | +class TipeeestreamConnector(override val sourceIdentifier: String) extends Connector(sourceIdentifier) with WithLogger { |
| 16 | + private val TIMEOUT = 10000 |
| 17 | + private val tipeeeStreamListener = new TipeeestreamListener |
| 18 | + override protected var requiredCredentialKeys: List[String] = List("apiKey", "username") |
| 19 | + override protected var optionalCredentialKeys: List[String] = List() |
| 20 | + private var socket: Option[Socket] = None |
| 21 | + |
| 22 | + override def start(): Boolean = { |
| 23 | + //RestAPI doesn't need stratup methods |
| 24 | + startSocket() |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Start the socket.io socket |
| 29 | + * |
| 30 | + * @return if the socket could start successfully |
| 31 | + */ |
| 32 | + private def startSocket(): Boolean = { |
| 33 | + var connected: Option[Boolean] = None |
| 34 | + val thread = Thread.currentThread |
| 35 | + socket.get.on(Socket.EVENT_CONNECT, (_: Any) => { |
| 36 | + logger info "Connected to TipeeStream Socket.io" |
| 37 | + socket.get.emit("join-room", AUTH_OBJECT) |
| 38 | + logger info "emitted credentials to TipeeSetream Socket.io api" |
| 39 | + socket.get.on("new-event", (objects: Array[AnyRef]) => { |
| 40 | + tipeeeStreamListener.onSocketEvent(objects) |
| 41 | + }) |
| 42 | + connected = Some(true) |
| 43 | + connected.notifyAll() |
| 44 | + }) |
| 45 | + socket.get.on(Socket.EVENT_CONNECT_ERROR, (e: Any) => { |
| 46 | + logger warn s"Could not connect to TipeeeStream socket:" |
| 47 | + logger warn e.asInstanceOf[Array[Object]].mkString(",") |
| 48 | + connected = Some(false) |
| 49 | + connected.notifyAll() |
| 50 | + }) |
| 51 | + socket.get.on(Socket.EVENT_CONNECT_TIMEOUT, (_: Any) => { |
| 52 | + logger warn s"$sourceIdentifier socket timed out" |
| 53 | + }) |
| 54 | + socket.get.on(Socket.EVENT_ERROR, (e: Any) => { |
| 55 | + logger warn s"$sourceIdentifier socket error:" |
| 56 | + e match { |
| 57 | + case array: Array[Any] => logger warn array.mkString(", ") |
| 58 | + case other => logger warn other.toString |
| 59 | + } |
| 60 | + }) |
| 61 | + val start = Calendar.getInstance.getTimeInMillis |
| 62 | + while (connected.isEmpty && start + TIMEOUT > Calendar.getInstance.getTimeInMillis) connected.wait(TIMEOUT) |
| 63 | + connected.getOrElse({ |
| 64 | + logger warn "Could not connect to TipeeeStream socket: Timed out!" |
| 65 | + false |
| 66 | + }) |
| 67 | + } |
| 68 | + |
| 69 | + private def AUTH_OBJECT: JSONObject = { |
| 70 | + val obj = new JSONObject() |
| 71 | + obj.put("room", credentials.get.getValue("apiKey").get) |
| 72 | + obj.put("username", credentials.get.getValue("username").get) |
| 73 | + obj |
| 74 | + } |
| 75 | + |
| 76 | + def addSubscriptionEventListener(listener: JSONObject => Unit): Unit = tipeeeStreamListener.addSubscriptionEventListener(listener) |
| 77 | + |
| 78 | + def addDonationEventListener(listener: JSONObject => Unit): Unit = tipeeeStreamListener.addDonationEventListener(listener) |
| 79 | + |
| 80 | + def addFollowEventListener(listener: JSONObject => Unit): Unit = tipeeeStreamListener.addFollowEventListener(listener) |
| 81 | + |
| 82 | + override def stop(): Boolean = { |
| 83 | + socket.foreach(_.close()) |
| 84 | + true |
| 85 | + } |
| 86 | +} |
0 commit comments