
12.10.2024, 18:48
|
|
Познавший АНТИЧАТ
Регистрация: 05.03.2017
Сообщений: 1,397
С нами:
4837380
Репутация:
168
|
|
SERVER WebSocket + Hono + Bun:
JavaScript :
Код:
import
{
Hono
}
from
"hono"
;
import
{
createBunWebSocket
}
from
"hono/bun"
;
import
type
{
ServerWebSocket
}
from
"bun"
;
const
{
upgradeWebSocket
,
websocket
}
=
createBunWebSocket
(
)
;
const
app
=
new
Hono
(
)
;
app
.
get
(
"/yourwebsocket"
,
upgradeWebSocket
(
(
c
)
=>
{
return
{
onOpen
(
event, ws
)
{
console
.
log
(
`New client:`
,
event
,
ws
)
;
}
,
onMessage
(
event, ws
)
{
console
.
log
(
`Message from client:${event.data}`
)
;
ws
.
send
(
"Hello from server! Привет мир!"
)
;
}
,
onClose
(
event, ws
)
{
console
.
log
(
"Connection closed"
)
;
}
,
}
;
}
)
)
;
export
default
{
fetch
:
app
.
fetch
,
websocket
,
}
;
CLIENTRakSAMP + Библиотеки в libs и в корень папки (там где стоит .exe):
(архив в конце поста ссылки)
Lua:
Код:
require
(
'addon'
)
local
encoding
=
require
(
'encoding'
)
;
encoding
.
default
=
'CP1251'
;
local
u8
=
encoding
.
UTF8
local
ws
=
require
(
'websocketsamp'
)
local
last_message
=
''
local
function
runWebSocket
(
)
return
newTask
(
function
(
)
while
true
do
if
ws
.
GetConnectionStatus
(
)
==
'OPEN'
then
local
output
=
ws
.
GetMessage
(
)
if
output
~=
''
and
output
~=
last_message
then
print
(
u8
:
decode
(
output
)
)
end
else
ws
.
Connect
(
'ws://localhost:3000/yourwebsocket'
)
;
end
wait
(
100
)
end
end
)
end
-- for dev
function
onRequestConnect
(
)
return
false
end
function
onLoad
(
)
local
task
=
runWebSocket
(
)
end
function
onInput
(
cmd
)
print
(
cmd
)
if
cmd
:
find
(
'!w (.+)'
)
then
last_message
=
cmd
:
match
(
'!w (.+)'
)
ws
.
SendMessage
(
u8
:
encode
(
last_message
)
)
return
false
end
end
function
onDisconnect
(
)
ws
.
Disconnect
(
)
end
|
|
|