Skip to content

Commit

Permalink
add ChatApplication class to keep a global socket
Browse files Browse the repository at this point in the history
  • Loading branch information
nkzawa committed Feb 13, 2016
1 parent 184dd92 commit 8f66b94
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<application
android:allowBackup="true"
android:name="com.github.nkzawa.socketio.androidchat.ChatApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.nkzawa.socketio.androidchat;

import android.app.Application;
import io.socket.client.IO;
import io.socket.client.Socket;

import java.net.URISyntaxException;

public class ChatApplication extends Application {

private Socket mSocket;
{
try {
mSocket = IO.socket(Constants.CHAT_SERVER_URL);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}

public Socket getSocket() {
return mSocket;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import io.socket.emitter.Emitter;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URISyntaxException;


/**
* A login screen that offers login via username.
Expand All @@ -30,19 +27,15 @@ public class LoginActivity extends Activity {
private String mUsername;

private Socket mSocket;
{
try {
mSocket = IO.socket(Constants.CHAT_SERVER_URL);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

ChatApplication app = (ChatApplication) getApplication();
mSocket = app.getSocket();

// Set up the login form.
mUsernameView = (EditText) findViewById(R.id.username_input);
mUsernameView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.*;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import io.socket.emitter.Emitter;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import org.json.JSONException;
import org.json.JSONObject;

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

Expand All @@ -44,13 +48,6 @@ public class MainFragment extends Fragment {
private Handler mTypingHandler = new Handler();
private String mUsername;
private Socket mSocket;
{
try {
mSocket = IO.socket(Constants.CHAT_SERVER_URL);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}

public MainFragment() {
super();
Expand All @@ -68,6 +65,8 @@ public void onCreate(Bundle savedInstanceState) {

setHasOptionsMenu(true);

ChatApplication app = (ChatApplication) getActivity().getApplication();
mSocket = app.getSocket();
mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
mSocket.on("new message", onNewMessage);
Expand Down

1 comment on commit 8f66b94

@huatrung181
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you show me the code of "http://chat.socket.io", I want to know the way is proccess server when it listen a new message or a new user join to server. thank you!

Please sign in to comment.