简体   繁体   English

如何使用网络套接字

[英]How to use web sockets

I'm trying to make use of Easy Websockets to come up with a chat application. 我正在尝试使用Easy Websockets来提出聊天应用程序。 http://easywebsocket.org/ http://easywebsocket.org/

Here's the code I have right now. 这是我现在的代码。 As you can see, I'm trying to log the message on the console every time I click on the send button. 如您所见,我每次单击“发送”按钮时都会尝试在控制台上记录消息。 It works when I open it up on 2 browsers. 当我在2个浏览器上打开它时,它可以工作。 But it only works for less than 1 minute. 但它只能工作不到1分钟。

<input type="text" id="txt"/>
<input type="button" value="send" id="sends"/>
<div id="messages"></div>

<script src="jquery171.js"></script>
<script src="easywebsockets.js"></script>   
<script>
    var socket = new EasyWebSocket("ws://sample.com/resource");//I do not understand this part
    socket.onopen   = function(){
        socket.send("hello world.")
    }
    socket.onmessage= function(event){
        console.log(event.data)
    }

    $('#sends').click(function(){
        var txt = $('#txt').val()
        socket.send(txt)
    });
</script>

I got this error log from firebug: 我从firebug得到了这个错误日志:

在此输入图像描述

I don't really understand what this all means. 我真的不明白这一切意味着什么。 Is there something I need to setup in order to make this work? 为了使这项工作,我需要设置一些东西吗?

It usually happens when the client doesn't use the same protocol version as the server. 当客户端不使用与服务器相同的协议版本时,通常会发生这种情况。 The server should specify what version it uses, socket.io by default goes with RFC6455 but it can be overridden to go with older hybi-0x. 服务器应该指定它使用的版本,socket.io默认使用RFC6455,但可以覆盖它以使用较旧的hybi-0x。

So, check the following: 所以,检查以下内容:

  • client & server implemented protocol versions 客户端和服务器实现协议版本
  • origin (eg localhost) is permitted by server 服务器允许origin(例如localhost)
  • firewalls, internet security suites, zonealarms, that kind of stuff 防火墙,互联网安全套件,zonealarms,那种东西

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

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