banner
NEWS LETTER

Android-SignalR使用

Scroll down

前言

SignalR的版本问题

服务端的版本和客户端的版本一定要匹配

添加依赖

1
2
3
implementation 'com.microsoft.signalr:signalr:5.0.9'
//下面这个是日志输出,上面这个库用到了这个
implementation group: 'org.slf4j', name: 'slf4j-android', version: '1.7.32'

创建布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/send" />

<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/edit_text"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="@id/send"
app:layout_constraintEnd_toStartOf="@id/send"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/send" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="Send"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

创建活动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class MainActivity : AppCompatActivity() {
private val coroutineScope = CoroutineScope(Dispatchers.Default)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val hubConnection = HubConnectionBuilder.create("http://192.168.1.10:8020/chathub")
.withTransport(TransportEnum.LONG_POLLING).build()
val listview = findViewById<ListView>(R.id.listview)
val editText = findViewById<AppCompatEditText>(R.id.edit_text)
val messageList: List<String> = ArrayList()
val arrayAdapter = ArrayAdapter(
this@MainActivity,
android.R.layout.simple_list_item_1, messageList
)
listview.adapter = arrayAdapter

hubConnection.on("ReceiveMessage", { user: String?, message: String? ->
runOnUiThread {
arrayAdapter.add("ReceiveMessage:${user}--${message}")
arrayAdapter.notifyDataSetChanged()
}
}, String::class.java, String::class.java)

findViewById<AppCompatButton>(R.id.send).setOnClickListener {
val message = editText.text.toString()
if (message.isEmpty()) {
return@setOnClickListener
}
editText.setText("")
try {
hubConnection.send("SendMessage", "Android--", message)
arrayAdapter.add("SendMessage:${message}")
arrayAdapter.notifyDataSetChanged()
} catch (e: Exception) {
e.printStackTrace()
}
}
coroutineScope.launch {
connect(hubConnection, arrayAdapter)
}
}

override fun onDestroy() {
super.onDestroy()
coroutineScope.cancel()
}

private suspend fun connect(hubConnection: HubConnection, arrayAdapter: ArrayAdapter<String>) {
try {
withContext(Dispatchers.Default) {
hubConnection.start().blockingAwait()
arrayAdapter.add(
"connectionState:${hubConnection.connectionState}" +
"\nconnectionId:${hubConnection.connectionId}" +
"\nkeepAliveInterval:${hubConnection.keepAliveInterval}" +
"\nserverTimeout:${hubConnection.serverTimeout}"
)
arrayAdapter.notifyDataSetChanged()
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}

混淆

1
2
# SignalR
-keep class com.microsoft.**{*;}
其他文章
目录导航 置顶
  1. 1. 前言
请输入关键词进行搜索