简体   繁体   English

LuaSocket-TCP第二消息未发送

[英]LuaSocket - TCP 2nd message not sending

I've been searching Google for awhile and there seems to be no offers on fixing this problem I have here. 我一直在搜索Google一段时间,看来这里没有解决此问题的报价。

I am using LuaSocket as a simple way to connect to a external server I created, and I am able to connect to it successfully and send a signal. 我使用LuaSocket作为连接到我创建的外部服务器的简单方法,并且能够成功连接到它并发送信号。

However, when I try to send a second message later on, the external server does not seem to be receiving the message, even though I am still connected to the socket. 但是,当我稍后尝试发送第二条消息时,即使我仍连接到套接字,外部服务器似乎也未收到该消息。

socket = require("socket")
host, port = ip, port
tcp = assert(socket.tcp())
tcp:settimeout( 0 )

tcp:connect(host, port);

msg = {
    ["status"]="connect",
    ["usrName"]=username
}
msg = Json.Encode(msg)

tcp:send(msg); -- This message, the server received this message.


-- Later in my code, I attempt to send another message.

msg = {
    ["status"]="anotherMessage",
    ["usrName"]=username
};
msg = Json.Encode(msg) 
tcp:send(msg); -- This message is not sending, even though i'm still connected.

You need to show what happens on the other side as it may be simply not reading even though the connection may be open. 您需要显示另一侧发生的情况,因为即使连接可能已打开,也可能根本无法读取。 You also don't say what exactly happens when "message is not sending"; 您也不会说“消息未发送”时到底发生了什么。 do you get an error? 你得到一个错误吗? the script finishes but the message is not sent? 脚本完成,但消息未发送?

There are several things you can try: 您可以尝试以下几种方法:

  • Switch to the (default) synchronous send until you get it working; 切换到(默认)同步发送,直到您开始工作为止; remove tcp:settimeout(0) , as your send may simply fail with "timeout" message if the other side is not ready to read the message. 删除tcp:settimeout(0) ,因为如果另一端还没有准备好读取消息,则发送可能会因“超时”消息而失败。
  • Check the error message from :send call to see if it's timing out or not. 检查来自:send调用的错误消息,以查看是否超时。

    local ok, err = tcp:send(msg)

  • Use socket.select to check if the other side it ready to accept the message you are sending. 使用socket.select来检查另一侧是否已准备好接受您发送的消息。

尝试在序列化JSON的末尾添加"\\r\\n"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM