Navigation Menu

Skip to content

Commit

Permalink
socket shit
Browse files Browse the repository at this point in the history
  • Loading branch information
ericzhang98 committed Oct 22, 2017
1 parent b53f2d5 commit 202af04
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle
Expand Up @@ -28,6 +28,7 @@ dependencies {
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'de.hdodenhof:circleimageview:2.2.0'
compile 'com.google.firebase:firebase-database:11.4.2' //Firebase database
compile 'com.github.nkzawa:socket.io-client:0.3.0' //Socket.io
testCompile 'junit:junit:4.12'
}

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.isaacwang.wholesomeapp">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
Expand Up @@ -27,6 +27,7 @@
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.ValueEventListener;
Expand All @@ -53,27 +54,27 @@ protected void onCreate(Bundle savedInstanceState) {

feedLayout = (LinearLayout) findViewById(R.id.feedLayout);

FeedItem testFeedItem1 = new ConversationFeedItem("Jason");
FeedItem testFeedItem1 = new ConversationFeedItem("asdf");
FeedItem testFeedItem2 = new ConversationFeedItem("Alvin");
FeedItem testFeedItem3 = new ConversationFeedItem("Rick");
initializeFeed(Arrays.asList(testFeedItem1, testFeedItem2, testFeedItem3));

Network.addToFeed(testFeedItem1);
Network.addToFeed(testFeedItem2);
Network.addToFeed(testFeedItem3);
// Network.addToFeed(testFeedItem1);

Network.feedDatabase.addValueEventListener(new ValueEventListener() {
// attach FeedDatabase listener
final Context context = this;
Network.feedDatabase.addChildEventListener(new ChildEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<FeedItem> feedList = new ArrayList<FeedItem>();
for (DataSnapshot feedSnapshot : dataSnapshot.getChildren()) {
FeedItem feedItem = feedSnapshot.getValue(ConversationFeedItem.class);
feedList.add(feedItem);
}
System.out.println("Feed list: " + feedList);
initializeFeed(feedList);
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
FeedItem feedItem = dataSnapshot.getValue(ConversationFeedItem.class);
feedLayout.addView(feedItem.getView(context, feedLayout), 0);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {}
@Override
public void onCancelled(DatabaseError databaseError) {}
});

Expand Down Expand Up @@ -102,6 +103,9 @@ public void startChat(View v) {
Intent i = new Intent(this, ChatActivity.class);
i.putExtra("partner_name", "Jason");
startActivity(i);

Network.wantToTalk();
System.out.println("want to talk");
}


Expand Down
52 changes: 52 additions & 0 deletions app/src/main/java/com/example/isaacwang/wholesomeapp/Network.java
@@ -1,11 +1,20 @@
package com.example.isaacwang.wholesomeapp;

import android.app.Activity;

import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.URISyntaxException;
import java.util.ArrayList;

/**
Expand Down Expand Up @@ -37,4 +46,47 @@ public static void addToFeed(FeedItem feedItem) {
System.out.println("FEED ITEM: " + feedItem);
feedDatabase.push().setValue(feedItem);
}

public static Socket chatSocket;

public static void wantToTalk() {
try {
chatSocket = IO.socket("http://10.0.2.2:3000");
chatSocket.connect();
chatSocket.emit("want_to_talk", "MY_ID");
} catch(URISyntaxException e) {System.out.println("Failed to connect to socket");}
}

public static void downToListen(Emitter.Listener listener) {
try {
chatSocket = IO.socket("10.0.2.2:3000");
chatSocket.connect();
chatSocket.emit("down_to_listen", "MY_ID");

// Emitter.Listener onNewTalker = new Emitter.Listener() {
// @Override
// public void call(final Object... args) {
// activity.runOnUiThread(new Runnable() {
// @Override
// public void run() {
// JSONObject data = (JSONObject) args[0];
// String username;
// String message;
// try {
// username = data.getString("username");
// message = data.getString("message");
// } catch (JSONException e) {
// return;
// }
//
// // add the message to view
// addMessage(username, message);
// }
// });
// }
// };
chatSocket.on("want_to_listen", listener);
} catch(URISyntaxException e) {}
}

}

0 comments on commit 202af04

Please sign in to comment.