Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

怎么通过Websocket建立长连接 #1707

Closed
mishuawu opened this issue Feb 23, 2023 · 0 comments
Closed

怎么通过Websocket建立长连接 #1707

mishuawu opened this issue Feb 23, 2023 · 0 comments

Comments

@mishuawu
Copy link

今天看到Skynet的示例里面有个Websocket的示例,我尝试运行了下,可以开启服务

略微修改了一点代码,全部代码如下
local skynet = require "skynet"
local socket = require "skynet.socket"
local service = require "skynet.service"
local websocket = require "http.websocket"
local handle = {}
local MODE = ...
if MODE == "agent" then
function handle.connect(id)
print("ws connect from: " .. tostring(id))
end
function handle.handshake(id, header, url)
local addr = websocket.addrinfo(id)
print("ws handshake from: " .. tostring(id), "url", url, "addr:", addr)
print("----header-----")
for k,v in pairs(header) do
print(k,v)
end
print("--------------")
end
function handle.message(id, msg, msg_type)
assert(msg_type == "binary" or msg_type == "text")
websocket.write(id, msg)
end
function handle.ping(id)
print("ws ping from: " .. tostring(id) .. "\n")
end
function handle.pong(id)
print("ws pong from: " .. tostring(id))
end
function handle.close(id, code, reason)
print("ws close from: " .. tostring(id), code, reason)
end
function handle.error(id)
print("ws error from: " .. tostring(id))
end
skynet.start(function ()
skynet.dispatch("lua", function (,, id, protocol, addr)
local ok, err = websocket.accept(id, handle, protocol, addr)
if not ok then
print(err)
end
end)
end)
else
local function simple_echo_client_service(protocol)
local skynet = require "skynet"
local websocket = require "http.websocket"
local url = string.format("%s://192.168.208.133:9948/test_websocket", protocol)
local ws_id = websocket.connect(url)
print("url ==============================="..url);
while true do
local msg = "hello world!"
websocket.write(ws_id, msg)
print(">: " .. msg)
local resp, close_reason = websocket.read(ws_id)
print("<: " .. (resp and resp or "[Close] " .. close_reason))
if not resp then
print("echo server close.")
break
end
websocket.ping(ws_id)
skynet.sleep(100)
end
end
skynet.start(function ()
local agent = {}
for i= 1, 1 do
agent[i] = skynet.newservice(SERVICE_NAME, "agent")
end
local balance = 1
local protocol = "ws"
local id = socket.listen("0.0.0.0", 9948)
skynet.error(string.format("Listen websocket port 9948 protocol:%s", protocol))
socket.start(id, function(id, addr)
print(string.format("accept client socket_id: %s addr:%s", id, addr))
skynet.send(agent[balance], "lua", id, protocol, addr)
-- balance = balance + 1
-- if balance > #agent then
-- balance = 1
-- end
end)
-- test echo client
service.new("websocket_echo_client", simple_echo_client_service, protocol)
end)
end
----------------------------------------------代码结束----------------------------------------------------
通过skynet开启这个服务
[:01000002] LAUNCH snlua bootstrap
[:01000003] LAUNCH snlua launcher
[:01000004] LAUNCH snlua cmaster
[:01000004] master listen socket 0.0.0.0:2013
[:01000005] LAUNCH snlua cslave
[:01000005] slave connect to master 127.0.0.1:2013
[:01000004] connect from 127.0.0.1:39886 4
[:01000006] LAUNCH harbor 1 16777221
[:01000004] Harbor 1 (fd=4) report 127.0.0.1:2526
[:01000005] Waiting for 0 harbors
[:01000005] Shakehand ready
[:01000007] LAUNCH snlua datacenterd
[:01000008] LAUNCH snlua service_mgr
[:01000009] LAUNCH snlua 2_cocos_test
[:0100000a] LAUNCH snlua 2_cocos_test agent
[:01000009] Listen websocket port 9948 protocol:ws
[:0100000b] LAUNCH snlua service_provider
[:0100000c] LAUNCH snlua service_cell websocket_echo_client
accept client socket_id: 7 addr:192.168.208.133:54408
ws connect from: 7
ws handshake from: 7 url /test_websocket addr: 192.168.208.133:54408
----header-----
host 192.168.208.133
connection Upgrade
sec-websocket-version 13
content-length 0
upgrade websocket
sec-websocket-key EFutW65CDNAjovZv8VCPiQ==
url ===============================ws://192.168.208.133:8888/test_websocket

: hello world!
<: hello world!
ws ping from: 7
: hello world!
<: hello world!
ws ping from: 7
...
...
...


看起来这个服务完全没问题,于是我打算通过前端通过Websocket建立一个长连接
前端代码如下(typescript):
let socket = new WebSocket("ws://192.168.208.133:9948/test_websocket");//192.168.208.133
socket.onopen = this.onOpen;
socket.onclose = this.onClose;
socket.onmessage = this.onMessage;
socket.onerror = this.onError;
onOpen(ev: Event) {
console.log("onOpen:", ev);
}
onClose(ev: CloseEvent) {
console.log("onClose:", ev);
}
onMessage(ev: MessageEvent) {
console.log("onMessage:", ev);
}
onError(ev: Event) {
console.log("onError:", ev);
}
---------------------------------------代码结束-----------------------------------------------------
结果运行,连接不上
target: WebSocket
binaryType: "blob"
bufferedAmount: 0
extensions: ""
onclose: ƒ onClose(ev)
onerror: ƒ onError(ev)
onmessage: ƒ onMessage(ev)
onopen: ƒ onOpen(ev)
protocol: ""
readyState: 3
url: "ws://192.168.208.133:9948/test_websocket"

希望帮忙看下可能是哪里的问题

Repository owner locked and limited conversation to collaborators Feb 23, 2023
@cloudwu cloudwu converted this issue into discussion #1708 Feb 23, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant