Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
p x
SuZhouAvp
Commits
816a069a
Commit
816a069a
authored
Jun 10, 2025
by
p x
Browse files
添加websocket
parent
1ccbc004
Changes
30
Hide whitespace changes
Inline
Side-by-side
app/src/main/java/com/sd/cavphmi/viewmodels/MainVm.kt
0 → 100644
View file @
816a069a
package
com.sd.cavphmi.viewmodels
import
android.content.Context
import
androidx.lifecycle.viewModelScope
import
com.sd.cavphmi.base.MyBaseViewModel
import
com.sd.cavphmi.bean.SpaceNoBean
import
com.sd.cavphmi.bean.VToXImgBean
import
com.sd.cavphmi.net.MyResult
import
com.sd.cavphmi.repositorys.SpaceNoRepo
import
com.sd.cavphmi.utils.MyContants
import
com.sd.cavphmi.utils.ToastHelper
import
com.sd.cavphmi.websockets.MyWebSocketClient
import
dagger.hilt.android.lifecycle.HiltViewModel
import
dagger.hilt.android.qualifiers.ApplicationContext
import
kotlinx.coroutines.Dispatchers
import
kotlinx.coroutines.launch
import
org.java_websocket.client.WebSocketClient
import
java.net.URI
import
javax.inject.Inject
@HiltViewModel
class
MainVm
@Inject
constructor
(
@ApplicationContext
var
context
:
Context
,
var
spaceNoRepo
:
SpaceNoRepo
)
:
MyBaseViewModel
()
{
private
var
client
:
MyWebSocketClient
?
=
null
fun
startWS
()
{
val
httpHeaders
=
mutableMapOf
<
String
,
String
>()
httpHeaders
.
put
(
"Cookie"
,
"username=nemo"
)
client
=
MyWebSocketClient
(
URI
(
MyContants
.
WS_HOST
))
client
?.
connect
()
}
fun
closeWS
()
{
if
(
client
?.
isOpen
?:
false
)
{
client
?.
close
()
}
}
fun
showVToTip
()
{
var
imgs
=
VToXImgBean
.
imgs
ToastHelper
.
showCustViewShort
(
context
,
imgs
.
get
(
"2"
)
!!
)
}
fun
getSpaceData
()
{
viewModelScope
.
launch
{
var
result
=
spaceNoRepo
.
getSpaceData
()
when
(
result
)
{
is
MyResult
.
Success
<
SpaceNoBean
>
->
{
var
a
=
0
}
else
->
{
var
b
=
0
}
}
}
}
}
\ No newline at end of file
app/src/main/java/com/sd/cavphmi/websockets/MyWebSocketClient.kt
0 → 100644
View file @
816a069a
package
com.sd.cavphmi.websockets
import
org.java_websocket.client.WebSocketClient
import
org.java_websocket.drafts.Draft
import
org.java_websocket.handshake.ServerHandshake
import
java.lang.Exception
import
java.net.URI
import
java.nio.ByteBuffer
class
MyWebSocketClient
:
WebSocketClient
{
val
TAG
=
"-----MyWebSocketClient"
constructor
(
serverUri
:
URI
?)
:
super
(
serverUri
)
constructor
(
serverUri
:
URI
?,
protocolDraft
:
Draft
?)
:
super
(
serverUri
,
protocolDraft
)
constructor
(
serverUri
:
URI
?,
httpHeaders
:
MutableMap
<
String
,
String
>?)
:
super
(
serverUri
,
httpHeaders
)
override
fun
onOpen
(
handshakedata
:
ServerHandshake
?)
{
send
(
"Hello, it is me. Mario :)"
)
System
.
out
.
println
(
TAG
+
"new connection opened"
)
}
override
fun
onMessage
(
message
:
String
?)
{
System
.
out
.
println
(
TAG
+
"received message: "
+
message
);
//处理各种推送消息
// let data = {};
// let result = await decodeResponseProtoBuf(msg);
// // console.log(result);
//
// // 抹平与之前推送数据接口的差异
// data.body = result.data;
// const eventName = this.dic[result.msgType].action;
//
// //执行回调
// // this.messageList[eventName](data);
// if (this.messageList[eventName]) {
// //执行回调
// this.messageList[eventName](data);
// }
}
override
fun
onMessage
(
bytes
:
ByteBuffer
?)
{
System
.
out
.
println
(
TAG
+
"received ByteBuffer"
)
}
override
fun
onClose
(
code
:
Int
,
reason
:
String
?,
remote
:
Boolean
)
{
System
.
out
.
println
(
TAG
+
"closed with exit code "
+
code
+
" additional info: "
+
reason
);
}
override
fun
onError
(
ex
:
Exception
?)
{
System
.
err
.
println
(
TAG
+
"an error occurred:"
+
ex
);
}
}
\ No newline at end of file
app/src/main/res/drawable-xhdpi/abnormal.png
0 → 100644
View file @
816a069a
6.66 KB
app/src/main/res/drawable-xhdpi/admission.png
0 → 100644
View file @
816a069a
6.97 KB
app/src/main/res/drawable-xhdpi/advance.png
0 → 100644
View file @
816a069a
6.6 KB
app/src/main/res/drawable-xhdpi/change_lanes.png
0 → 100644
View file @
816a069a
6.63 KB
app/src/main/res/drawable-xhdpi/intersection.png
0 → 100644
View file @
816a069a
7.46 KB
app/src/main/res/drawable-xhdpi/weak.png
0 → 100644
View file @
816a069a
9.1 KB
app/src/main/res/layout/activity_main.xml
View file @
816a069a
...
...
@@ -7,7 +7,7 @@
<variable
name=
"vm"
type=
"com.sd.cavphmi.
base.MyBaseV
iew
M
odel"
/>
type=
"com.sd.cavphmi.
v
iew
m
odel
s.MainVm
"
/>
</data>
...
...
app/src/main/res/layout/toast_tip.xml
0 → 100644
View file @
816a069a
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"
android:orientation=
"vertical"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
>
<ImageView
android:id=
"@+id/img_tip"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
/>
</LinearLayout>
\ No newline at end of file
Prev
1
2
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment