mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
add websocket address and url param
This commit is contained in:
@@ -11,8 +11,9 @@ if MODE == "agent" then
|
|||||||
print("ws connect from: " .. tostring(id))
|
print("ws connect from: " .. tostring(id))
|
||||||
end
|
end
|
||||||
|
|
||||||
function handle.handshake(id, header)
|
function handle.handshake(id, header, url)
|
||||||
print("ws handshake from: " .. tostring(id))
|
local addr = websocket.addrinfo(id)
|
||||||
|
print("ws handshake from: " .. tostring(id), "url", url, "addr:", addr)
|
||||||
print("----header-----")
|
print("----header-----")
|
||||||
for k,v in pairs(header) do
|
for k,v in pairs(header) do
|
||||||
print(k,v)
|
print(k,v)
|
||||||
@@ -41,8 +42,8 @@ if MODE == "agent" then
|
|||||||
end
|
end
|
||||||
|
|
||||||
skynet.start(function ()
|
skynet.start(function ()
|
||||||
skynet.dispatch("lua", function (_,_, id, protocol)
|
skynet.dispatch("lua", function (_,_, id, protocol, addr)
|
||||||
websocket.accept(id, handle, protocol)
|
websocket.accept(id, handle, protocol, addr)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
@@ -50,7 +51,7 @@ else
|
|||||||
local function simple_echo_client_service(protocol)
|
local function simple_echo_client_service(protocol)
|
||||||
local skynet = require "skynet"
|
local skynet = require "skynet"
|
||||||
local websocket = require "http.websocket"
|
local websocket = require "http.websocket"
|
||||||
local url = string.format("%s://127.0.0.1:9948/", protocol)
|
local url = string.format("%s://127.0.0.1:9948/test_websocket", protocol)
|
||||||
local ws_id = websocket.connect(url)
|
local ws_id = websocket.connect(url)
|
||||||
while true do
|
while true do
|
||||||
local msg = "hello world!"
|
local msg = "hello world!"
|
||||||
@@ -78,7 +79,7 @@ else
|
|||||||
skynet.error(string.format("Listen websocket port 9948 protocol:%s", protocol))
|
skynet.error(string.format("Listen websocket port 9948 protocol:%s", protocol))
|
||||||
socket.start(id, function(id, addr)
|
socket.start(id, function(id, addr)
|
||||||
print(string.format("accept client socket_id: %s addr:%s", id, addr))
|
print(string.format("accept client socket_id: %s addr:%s", id, addr))
|
||||||
skynet.send(agent[balance], "lua", id, protocol)
|
skynet.send(agent[balance], "lua", id, protocol, addr)
|
||||||
balance = balance + 1
|
balance = balance + 1
|
||||||
if balance > #agent then
|
if balance > #agent then
|
||||||
balance = 1
|
balance = 1
|
||||||
|
|||||||
@@ -140,7 +140,7 @@ local function read_handshake(self)
|
|||||||
sub_pro ..
|
sub_pro ..
|
||||||
"\r\n"
|
"\r\n"
|
||||||
self.write(resp)
|
self.write(resp)
|
||||||
return nil, header
|
return nil, header, url
|
||||||
end
|
end
|
||||||
|
|
||||||
local function try_handle(self, method, ...)
|
local function try_handle(self, method, ...)
|
||||||
@@ -240,7 +240,7 @@ end
|
|||||||
|
|
||||||
local function resolve_accept(self)
|
local function resolve_accept(self)
|
||||||
try_handle(self, "connect")
|
try_handle(self, "connect")
|
||||||
local code, err = read_handshake(self)
|
local code, err, url = read_handshake(self)
|
||||||
if code then
|
if code then
|
||||||
local ok, s = httpd.write_response(self.write, code, err)
|
local ok, s = httpd.write_response(self.write, code, err)
|
||||||
if not ok then
|
if not ok then
|
||||||
@@ -249,7 +249,7 @@ local function resolve_accept(self)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local header = err
|
local header = err
|
||||||
try_handle(self, "handshake", header)
|
try_handle(self, "handshake", header, url)
|
||||||
local recv_count = 0
|
local recv_count = 0
|
||||||
local recv_buf = {}
|
local recv_buf = {}
|
||||||
while true do
|
while true do
|
||||||
@@ -381,10 +381,11 @@ end
|
|||||||
|
|
||||||
-- handle interface
|
-- handle interface
|
||||||
-- connect / handshake / message / ping / pong / close / error
|
-- connect / handshake / message / ping / pong / close / error
|
||||||
function M.accept(socket_id, handle, protocol)
|
function M.accept(socket_id, handle, protocol, addr)
|
||||||
socket.start(socket_id)
|
socket.start(socket_id)
|
||||||
protocol = protocol or "ws"
|
protocol = protocol or "ws"
|
||||||
local ws_obj = _new_server_ws(socket_id, handle, protocol)
|
local ws_obj = _new_server_ws(socket_id, handle, protocol)
|
||||||
|
ws_obj.addr = addr
|
||||||
local on_warning = handle and handle["warning"]
|
local on_warning = handle and handle["warning"]
|
||||||
if on_warning then
|
if on_warning then
|
||||||
socket.warning(socket_id, function (id, sz)
|
socket.warning(socket_id, function (id, sz)
|
||||||
@@ -427,6 +428,7 @@ function M.connect(url, header, timeout)
|
|||||||
uri = uri == "" and "/" or uri
|
uri = uri == "" and "/" or uri
|
||||||
local socket_id = sockethelper.connect(host_name, host_port, timeout)
|
local socket_id = sockethelper.connect(host_name, host_port, timeout)
|
||||||
local ws_obj = _new_client_ws(socket_id, protocol)
|
local ws_obj = _new_client_ws(socket_id, protocol)
|
||||||
|
ws_obj.addr = host
|
||||||
write_handshake(ws_obj, host_name, uri, header)
|
write_handshake(ws_obj, host_name, uri, header)
|
||||||
return socket_id
|
return socket_id
|
||||||
end
|
end
|
||||||
@@ -472,6 +474,10 @@ function M.ping(id)
|
|||||||
write_frame(ws_obj, "ping")
|
write_frame(ws_obj, "ping")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M.addrinfo(id)
|
||||||
|
local ws_obj = assert(ws_pool[id])
|
||||||
|
return ws_obj.addr
|
||||||
|
end
|
||||||
|
|
||||||
function M.close(id, code ,reason)
|
function M.close(id, code ,reason)
|
||||||
local ws_obj = ws_pool[id]
|
local ws_obj = ws_pool[id]
|
||||||
|
|||||||
Reference in New Issue
Block a user