Skip to content

Commit 9439614

Browse files
authored
Chat Example (#55)
* Chat example * Added example and fixed reconnect issues * bump client version * cleanup * Building using jar
1 parent 55e68ce commit 9439614

36 files changed

+814
-1
lines changed

ChatExample/.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild

ChatExample/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

ChatExample/app/build.gradle

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
apply plugin: 'com.android.application'
2+
3+
apply plugin: 'kotlin-android'
4+
5+
apply plugin: 'kotlin-android-extensions'
6+
7+
android {
8+
compileSdkVersion 28
9+
defaultConfig {
10+
applicationId "com.github.dsrees.chatexample"
11+
minSdkVersion 19
12+
targetSdkVersion 28
13+
versionCode 1
14+
versionName "1.0"
15+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
16+
}
17+
buildTypes {
18+
release {
19+
minifyEnabled false
20+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
21+
}
22+
}
23+
24+
compileOptions {
25+
targetCompatibility = "8"
26+
sourceCompatibility = "8"
27+
}
28+
}
29+
30+
dependencies {
31+
/*
32+
To update the JavaPhoenixClient, either use the latest dependency from jcenter
33+
OR run
34+
`./gradlew jar`
35+
and copy
36+
`/build/lib/*.jar` to `/ChatExample/app/libs`
37+
and comment out the jcenter dependency
38+
*/
39+
implementation fileTree(dir: 'libs', include: ['*.jar'])
40+
// implementation 'com.github.dsrees:JavaPhoenixClient:0.2.3'
41+
42+
43+
compile "com.google.code.gson:gson:2.8.5"
44+
compile "com.squareup.okhttp3:okhttp:3.12.2"
45+
46+
47+
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
48+
49+
implementation 'androidx.appcompat:appcompat:1.0.2'
50+
implementation 'androidx.recyclerview:recyclerview:1.0.0'
51+
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
52+
53+
}
104 KB
Binary file not shown.

ChatExample/app/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools" package="com.github.dsrees.chatexample">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
7+
<application
8+
android:allowBackup="true"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:roundIcon="@mipmap/ic_launcher_round"
12+
android:supportsRtl="true"
13+
android:networkSecurityConfig="@xml/network_security_config"
14+
android:theme="@style/AppTheme" tools:ignore="GoogleAppIndexingWarning">
15+
<activity android:name=".MainActivity">
16+
<intent-filter>
17+
<action android:name="android.intent.action.MAIN"/>
18+
19+
<category android:name="android.intent.category.LAUNCHER"/>
20+
</intent-filter>
21+
</activity>
22+
</application>
23+
24+
</manifest>
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package com.github.dsrees.chatexample
2+
3+
import androidx.appcompat.app.AppCompatActivity
4+
import android.os.Bundle
5+
import android.util.Log
6+
import android.widget.ArrayAdapter
7+
import android.widget.Button
8+
import android.widget.EditText
9+
import androidx.recyclerview.widget.LinearLayoutManager
10+
import kotlinx.android.synthetic.main.activity_main.*
11+
import org.phoenixframework.Channel
12+
import org.phoenixframework.Socket
13+
14+
class MainActivity : AppCompatActivity() {
15+
16+
companion object {
17+
const val TAG = "MainActivity"
18+
}
19+
20+
private val messagesAdapter = MessagesAdapter()
21+
private val layoutManager = LinearLayoutManager(this)
22+
23+
24+
// Use when connecting to https://github.com/dwyl/phoenix-chat-example
25+
private val socket = Socket("https://phxchat.herokuapp.com/socket/websocket")
26+
private val topic = "rooms:lobby"
27+
28+
// Use when connecting to local server
29+
// private val socket = Socket("ws://10.0.2.2:4000/socket/websocket")
30+
// private val topic = "room:lobby"
31+
32+
private var lobbyChannel: Channel? = null
33+
34+
private val username: String
35+
get() = username_input.text.toString()
36+
37+
private val message: String
38+
get() = message_input.text.toString()
39+
40+
override fun onCreate(savedInstanceState: Bundle?) {
41+
super.onCreate(savedInstanceState)
42+
setContentView(R.layout.activity_main)
43+
44+
45+
layoutManager.stackFromEnd = true
46+
47+
messages_recycler_view.layoutManager = layoutManager
48+
messages_recycler_view.adapter = messagesAdapter
49+
50+
socket.onOpen {
51+
this.addText("Socket Opened")
52+
runOnUiThread { connect_button.text = "Disconnect" }
53+
}
54+
55+
socket.onClose {
56+
this.addText("Socket Closed")
57+
runOnUiThread { connect_button.text = "Connect" }
58+
}
59+
60+
socket.onError { throwable, response ->
61+
Log.e(TAG, "Socket Errored $response", throwable)
62+
this.addText("Socket Error")
63+
}
64+
65+
socket.logger = {
66+
Log.d(TAG, "SOCKET $it")
67+
}
68+
69+
70+
connect_button.setOnClickListener {
71+
if (socket.isConnected) {
72+
this.disconnectAndLeave()
73+
} else {
74+
this.disconnectAndLeave()
75+
this.connectAndJoin()
76+
}
77+
}
78+
79+
send_button.setOnClickListener { sendMessage() }
80+
}
81+
82+
private fun sendMessage() {
83+
val payload = mapOf("user" to username, "body" to message)
84+
this.lobbyChannel?.push("new:msg", payload)
85+
?.receive("ok") { Log.d(TAG, "success $it") }
86+
?.receive("error") { Log.d(TAG, "error $it") }
87+
88+
message_input.text.clear()
89+
}
90+
91+
private fun disconnectAndLeave() {
92+
// Be sure the leave the channel or call socket.remove(lobbyChannel)
93+
lobbyChannel?.leave()
94+
socket.disconnect { this.addText("Socket Disconnected") }
95+
}
96+
97+
private fun connectAndJoin() {
98+
val channel = socket.channel(topic, mapOf("status" to "joining"))
99+
channel.on("join") {
100+
this.addText("You joined the room")
101+
}
102+
103+
channel.on("new:msg") { message ->
104+
val payload = message.payload
105+
val username = payload["user"] as? String
106+
val body = payload["body"]
107+
108+
109+
if (username != null && body != null) {
110+
this.addText("[$username] $body")
111+
}
112+
}
113+
114+
channel.on("user:entered") {
115+
this.addText("[anonymous entered]")
116+
}
117+
118+
this.lobbyChannel = channel
119+
channel
120+
.join()
121+
.receive("ok") {
122+
this.addText("Joined Channel")
123+
}
124+
.receive("error") {
125+
this.addText("Failed to join channel: ${it.payload}")
126+
}
127+
128+
129+
this.socket.connect()
130+
}
131+
132+
private fun addText(message: String) {
133+
runOnUiThread {
134+
this.messagesAdapter.add(message)
135+
layoutManager.smoothScrollToPosition(messages_recycler_view, null, messagesAdapter.itemCount)
136+
}
137+
138+
}
139+
140+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.github.dsrees.chatexample
2+
3+
import android.view.LayoutInflater
4+
import android.view.View
5+
import android.view.ViewGroup
6+
import android.widget.TextView
7+
import androidx.recyclerview.widget.RecyclerView
8+
9+
class MessagesAdapter : RecyclerView.Adapter<MessagesAdapter.ViewHolder>() {
10+
11+
private var messages: MutableList<String> = mutableListOf()
12+
13+
fun add(message: String) {
14+
messages.add(message)
15+
notifyItemInserted(messages.size)
16+
}
17+
18+
19+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
20+
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_message, parent, false)
21+
return ViewHolder(view)
22+
}
23+
24+
override fun getItemCount(): Int = messages.size
25+
26+
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
27+
holder.label.text = messages[position]
28+
}
29+
30+
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
31+
val label: TextView = itemView.findViewById(R.id.item_message_label)
32+
}
33+
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:aapt="http://schemas.android.com/aapt"
3+
android:width="108dp"
4+
android:height="108dp"
5+
android:viewportHeight="108"
6+
android:viewportWidth="108">
7+
<path
8+
android:fillType="evenOdd"
9+
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
10+
android:strokeColor="#00000000"
11+
android:strokeWidth="1">
12+
<aapt:attr name="android:fillColor">
13+
<gradient
14+
android:endX="78.5885"
15+
android:endY="90.9159"
16+
android:startX="48.7653"
17+
android:startY="61.0927"
18+
android:type="linear">
19+
<item
20+
android:color="#44000000"
21+
android:offset="0.0"/>
22+
<item
23+
android:color="#00000000"
24+
android:offset="1.0"/>
25+
</gradient>
26+
</aapt:attr>
27+
</path>
28+
<path
29+
android:fillColor="#FFFFFF"
30+
android:fillType="nonZero"
31+
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
32+
android:strokeColor="#00000000"
33+
android:strokeWidth="1"/>
34+
</vector>

0 commit comments

Comments
 (0)