{"action":"created","issue":{"url":"https://api.github.com/repos/Links2004/arduinoWebSockets/issues/221","repository_url":"https://api.github.com/repos/Links2004/arduinoWebSockets","labels_url":"https://api.github.com/repos/Links2004/arduinoWebSockets/issues/221/labels{/name}","comments_url":"https://api.github.com/repos/Links2004/arduinoWebSockets/issues/221/comments","events_url":"https://api.github.com/repos/Links2004/arduinoWebSockets/issues/221/events","html_url":"https://github.com/Links2004/arduinoWebSockets/issues/221","id":245130438,"number":221,"title":"Websocket server always calls callback function (Arduino Mega + Ethernet Shield)","user":{"login":"G-Hopper","id":30414002,"avatar_url":"https://avatars3.githubusercontent.com/u/30414002?v=4","gravatar_id":"","url":"https://api.github.com/users/G-Hopper","html_url":"https://github.com/G-Hopper","followers_url":"https://api.github.com/users/G-Hopper/followers","following_url":"https://api.github.com/users/G-Hopper/following{/other_user}","gists_url":"https://api.github.com/users/G-Hopper/gists{/gist_id}","starred_url":"https://api.github.com/users/G-Hopper/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/G-Hopper/subscriptions","organizations_url":"https://api.github.com/users/G-Hopper/orgs","repos_url":"https://api.github.com/users/G-Hopper/repos","events_url":"https://api.github.com/users/G-Hopper/events{/privacy}","received_events_url":"https://api.github.com/users/G-Hopper/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2017-07-24T16:07:15Z","updated_at":"2017-08-31T09:38:24Z","closed_at":null,"author_association":"NONE","body":"Hi!\r\n\r\nI am trying to create a small websocket echo server with this arduinoWebSockets library, but I just cannot make it work. When I start the program (code below) on the Arduino, it just starts calling the callback function endlessly with the WStype_t being 1 (WStype_DISCONNECTED). The code runs on an Arduino Mega with an Ethernet Shield.\r\nEdit: I am using the ATMega branch.\r\n\r\nThe source code:\r\n\r\n```\r\n#include <Ethernet.h>\r\n#include <WebSocketsServer.h>\r\n\r\nconst byte macAddress[] = { 0x02, 0x52, 0x3B, 0xE1, 0xCA, 0x19 };\r\n\r\nconst IPAddress IPAddressOfServer(192, 168, 1, 5);\r\nconst IPAddress networkMask(255, 255, 255, 255);\r\nconst IPAddress defaultGateway(192, 168, 1, 1);\r\nconst IPAddress DNSServer(8, 8, 8, 8);\r\n\r\nWebSocketsServer webSocketServer(8080);\r\n\r\nvoid webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {\r\n\t\r\n\tswitch (type)\r\n\t{\r\n\tcase WStype_CONNECTED:\r\n\t\tSerial.println(\"Connected\");\r\n\t\tbreak;\r\n\tcase WStype_DISCONNECTED:\r\n\t\tSerial.println(\"Connected\");\r\n\t\tbreak;\r\n\tcase WStype_ERROR:\r\n\t\tSerial.println(\"Error\");\r\n\t\tbreak;\r\n\tcase WStype_TEXT:\r\n\tcase WStype_BIN:\r\n\t\tSerial.println(\"Got text/bin\");\r\n\t\twebSocketServer.sendTXT(4, \"OK!\");\r\n\t\tbreak;\r\n\t}\r\n}\r\n\r\nvoid setup() {\r\n\tSerial.begin(921600);\r\n\r\n\tEthernet.begin(macAddress, IPAddressOfServer,\r\n\t\tDNSServer, defaultGateway, networkMask);\r\n\t\r\n\twebSocketServer.begin();\r\n\twebSocketServer.onEvent(webSocketEvent);\r\n\r\n\tSerial.println(\"started!\");\r\n}\r\n\r\nvoid loop() {\r\n\twebSocketServer.loop();\r\n}\r\n```\r\n\r\nSo right now I'm looking for some solution or guidance about what I'm doing wrong. I appreciate any help I can get!\r\nThank you!\r\n\r\nG."},"comment":{"url":"https://api.github.com/repos/Links2004/arduinoWebSockets/issues/comments/326244335","html_url":"https://github.com/Links2004/arduinoWebSockets/issues/221#issuecomment-326244335","issue_url":"https://api.github.com/repos/Links2004/arduinoWebSockets/issues/221","id":326244335,"user":{"login":"G-Hopper","id":30414002,"avatar_url":"https://avatars3.githubusercontent.com/u/30414002?v=4","gravatar_id":"","url":"https://api.github.com/users/G-Hopper","html_url":"https://github.com/G-Hopper","followers_url":"https://api.github.com/users/G-Hopper/followers","following_url":"https://api.github.com/users/G-Hopper/following{/other_user}","gists_url":"https://api.github.com/users/G-Hopper/gists{/gist_id}","starred_url":"https://api.github.com/users/G-Hopper/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/G-Hopper/subscriptions","organizations_url":"https://api.github.com/users/G-Hopper/orgs","repos_url":"https://api.github.com/users/G-Hopper/repos","events_url":"https://api.github.com/users/G-Hopper/events{/privacy}","received_events_url":"https://api.github.com/users/G-Hopper/received_events","type":"User","site_admin":false},"created_at":"2017-08-31T09:38:24Z","updated_at":"2017-08-31T09:38:24Z","author_association":"NONE","body":"Soo, I managed to discover some (I think) problems and made some (I think) solutions.\r\nYour code checks if there is data waiting with the NETWORK_ESP8266, but not with W5100, so the for cycle at 373 will always run.\r\nI added these lines after line 373,\r\n```\r\n#else\r\n\tif (_server->available()) {\r\n#endif\r\n```\r\nand commented the #if - #endif at line 422, 424.\r\nThis solved the continuous new client issue, but not the next one:\r\nhttp://prntscr.com/gfax5t\r\nAs the processing of the client headers is one header line at a time, the handleNewClients method will be called multiple times while a client is reading the header lines.\r\nTo solve this issue, modified the handleClientData method too. I replaced\r\n```\r\n int len = client->tcp->available();\r\n            if(len > 0) {\r\n```\r\nwith\r\n`while (client->tcp->available() > 0) {`\r\nand this one solved the next issue too. \r\nOf course we are not out of the water yet. This time the websocket server works almost fine, but:\r\nEvery time it gets a packet, as there is new data, _server->available() will be true and it will try to add a new client. Itt will not succeed of course, but the space will be given to this 'not real' new client, and it will try to decode the data:\r\nhttps://justpaste.it/1aq10\r\nThis is where I am standing now.\r\nAny thoughts?\r\n\r\nG."}}
{"actor":{"display_login":"G-Hopper"}}