simplewebsocket.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. local skynet = require "skynet"
  2. local socket = require "skynet.socket"
  3. local service = require "skynet.service"
  4. local websocket = require "http.websocket"
  5. local cjson = require "cjson"
  6. local cjson_encode = cjson.encode
  7. local handle = {}
  8. local MODE = ...
  9. -- if MODE == "agent" then
  10. -- function handle.connect(id)
  11. -- print("ws connect from: " .. tostring(id))
  12. -- end
  13. -- function handle.handshake(id, header, url)
  14. -- local addr = websocket.addrinfo(id)
  15. -- print("ws handshake from: " .. tostring(id), "url", url, "addr:", addr)
  16. -- print("----header-----")
  17. -- for k,v in pairs(header) do
  18. -- print(k,v)
  19. -- end
  20. -- print("--------------")
  21. -- end
  22. -- function handle.message(id, msg, msg_type)
  23. -- assert(msg_type == "binary" or msg_type == "text")
  24. -- websocket.write(id, msg)
  25. -- end
  26. -- function handle.ping(id)
  27. -- print("ws ping from: " .. tostring(id) .. "\n")
  28. -- end
  29. -- function handle.pong(id)
  30. -- print("ws pong from: " .. tostring(id))
  31. -- end
  32. -- function handle.close(id, code, reason)
  33. -- print("ws close from: " .. tostring(id), code, reason)
  34. -- end
  35. -- function handle.error(id)
  36. -- print("ws error from: " .. tostring(id))
  37. -- end
  38. -- skynet.start(function ()
  39. -- skynet.dispatch("lua", function (_,_, id, protocol, addr)
  40. -- local ok, err = websocket.accept(id, handle, protocol, addr)
  41. -- if not ok then
  42. -- print(err)
  43. -- end
  44. -- end)
  45. -- end)
  46. -- else
  47. -- local function simple_echo_client_service(protocol)
  48. -- local skynet = require "skynet"
  49. -- local websocket = require "http.websocket"
  50. -- local url = string.format("%s://127.0.0.1:9948/test_websocket", protocol)
  51. -- local ws_id = websocket.connect(url)
  52. -- while true do
  53. -- local msg = "hello world!"
  54. -- websocket.write(ws_id, msg)
  55. -- print(">: " .. msg)
  56. -- local resp, close_reason = websocket.read(ws_id)
  57. -- print("<: " .. (resp and resp or "[Close] " .. close_reason))
  58. -- if not resp then
  59. -- print("echo server close.")
  60. -- break
  61. -- end
  62. -- websocket.ping(ws_id)
  63. -- skynet.sleep(100)
  64. -- end
  65. -- end
  66. -- skynet.start(function ()
  67. -- local agent = {}
  68. -- for i= 1, 20 do
  69. -- agent[i] = skynet.newservice(SERVICE_NAME, "agent")
  70. -- end
  71. -- local balance = 1
  72. -- local protocol = "ws"
  73. -- local id = socket.listen("0.0.0.0", 9948)
  74. -- skynet.error(string.format("Listen websocket port 9948 protocol:%s", protocol))
  75. -- socket.start(id, function(id, addr)
  76. -- print(string.format("accept client socket_id: %s addr:%s", id, addr))
  77. -- skynet.send(agent[balance], "lua", id, protocol, addr)
  78. -- balance = balance + 1
  79. -- if balance > #agent then
  80. -- balance = 1
  81. -- end
  82. -- end)
  83. -- -- test echo client
  84. -- service.new("websocket_echo_client", simple_echo_client_service, protocol)
  85. -- end)
  86. -- end
  87. local function simple_echo_client_service(protocol)
  88. local skynet = require "skynet"
  89. local websocket = require "http.websocket"
  90. local url = string.format("%s://192.168.31.100:8001", protocol)
  91. print("echo server 3333333333333")
  92. skynet.sleep(100)
  93. local ws_id = websocket.connect(url)
  94. local tab = {
  95. type = "login",
  96. session = 1,
  97. content = {
  98. sid = 1,
  99. account = 4090,
  100. channel = "xiaohei",
  101. }
  102. }
  103. local msg = cjson_encode(tab)
  104. print(">: " .. msg)
  105. websocket.write(ws_id, msg)
  106. skynet.sleep(600)
  107. -- skynet.sleep(10000)
  108. -- websocket.ping(ws_id)
  109. -- websocket.write(ws_id, "hello")
  110. -- websocket.ping(ws_id)
  111. -- local msg = "hello world!"
  112. -- websocket.write(ws_id, msg)
  113. -- print(">: " .. msg)
  114. -- local resp, close_reason = websocket.read(ws_id)
  115. -- print("<: " .. (resp and resp or "[Close] " .. close_reason))
  116. -- if not resp then
  117. -- print("echo server close.")
  118. -- break
  119. -- end
  120. -- websocket.ping(ws_id)
  121. -- skynet.sleep(100)
  122. end
  123. skynet.start(function()
  124. simple_echo_client_service("ws")
  125. end)