Skip to content
This repository has been archived by the owner on Jul 21, 2020. It is now read-only.

Bunch of Event based changes for Chat/Constellation Websockets #82

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public static enum EventType {
@SerializedName("PollEnd") POLL_END (PollEndEvent.class),
@SerializedName("Stats") STATS (StatusEvent.class),
@SerializedName("UserJoin") USER_JOIN (UserJoinEvent.class),
@SerializedName("UserLeave") USER_LEAVE (UserLeaveEvent.class);
@SerializedName("UserLeave") USER_LEAVE (UserLeaveEvent.class),
@SerializedName("disconnect") DISCOUNNECT (ChatDisconnectEvent.class),
@SerializedName("WelcomeEvent") WELCOME (WelcomeEvent.class);

private final Class<? extends AbstractChatEvent> correspondingClass;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mixer.api.resource.chat.events;

import com.mixer.api.resource.chat.AbstractChatEvent;
import com.mixer.api.resource.chat.events.data.ChatDisconnectData;

public class ChatDisconnectEvent extends AbstractChatEvent<ChatDisconnectData> {
public ChatDisconnectEvent() {
this.event = EventType.DISCOUNNECT;
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/mixer/api/resource/chat/events/WelcomeEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mixer.api.resource.chat.events;

import com.mixer.api.resource.chat.AbstractChatEvent;
import com.mixer.api.resource.chat.events.data.WelcomeMessageData;

public class WelcomeEvent extends AbstractChatEvent<WelcomeMessageData> {
public WelcomeEvent() {
this.event = EventType.WELCOME;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.mixer.api.resource.chat.events.data;

import com.mixer.api.resource.chat.AbstractChatEvent;

public class ChatDisconnectData extends AbstractChatEvent.EventData {
public int code;
public String reason;
public boolean remote;

public ChatDisconnectData(int code, String reason, boolean remote) {
super();
this.code = code;
this.reason = reason;
this.remote = remote;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mixer.api.resource.chat.events.data;

import com.mixer.api.resource.chat.AbstractChatEvent;

public class WelcomeMessageData extends AbstractChatEvent.EventData {
public String server;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
Expand Down Expand Up @@ -136,6 +138,7 @@ public void onMessage(String s) {
}

@Override public void onClose(int i, String s, boolean b) {
this.close(i);
this.producer.notifyClose(i, s, b);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.mixer.api.resource.constellation;

import com.google.gson.annotations.SerializedName;
import com.mixer.api.resource.constellation.events.ConstellationDisconnectEvent;
import com.mixer.api.resource.constellation.events.HelloEvent;
import com.mixer.api.resource.constellation.events.LiveEvent;

public abstract class AbstractConstellationEvent<T extends AbstractConstellationEvent.EventData> extends AbstractConstellationDatagram {
Expand All @@ -13,7 +15,9 @@ public AbstractConstellationEvent() {

public static abstract class EventData {}
public static enum EventType {
@SerializedName("live") LIVE (LiveEvent.class);
@SerializedName("live") LIVE (LiveEvent.class),
@SerializedName("hello") HELLO (HelloEvent.class),
@SerializedName("disconnect") DISCOUNNECT (ConstellationDisconnectEvent.class);

private final Class<? extends AbstractConstellationEvent> correspondingClass;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mixer.api.resource.constellation.events;

import com.mixer.api.resource.constellation.AbstractConstellationEvent;
import com.mixer.api.resource.constellation.events.data.ConstellationDisconnectData;

public class ConstellationDisconnectEvent extends AbstractConstellationEvent<ConstellationDisconnectData> {
public ConstellationDisconnectEvent() {
this.type = Type.EVENT;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.mixer.api.resource.constellation.events.data;

import com.mixer.api.resource.constellation.AbstractConstellationEvent;

public class ConstellationDisconnectData extends AbstractConstellationEvent.EventData {
public int code;
public String reason;
public boolean remote;
public ConstellationDisconnectData(int code, String reason, boolean remote) {
super();
this.code = code;
this.reason = reason;
this.remote = remote;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
Expand Down Expand Up @@ -122,6 +124,18 @@ public void onMessage(String s) {
}

@Override public void onClose(int i, String s, boolean b) {
Class<? extends AbstractConstellationEvent> type = AbstractConstellationEvent.EventType
.fromSerializedName("disconnect").getCorrespondingClass();
Gson gson = null;
gson = new GsonBuilder().create();
JsonObject obj = new JsonObject();
obj.addProperty("code", i);
obj.addProperty("reason", s);
obj.addProperty("remote", b);
JsonObject json = new JsonObject();
json.add("data", obj);
this.dispatchEvent(this.mixer.gson.fromJson(json, type));
this.close(i);
this.producer.notifyClose(i, s, b);
}

Expand Down