0

I use websockets along with javascript and html5

I have the following code.

    <input type="text" onFocus=" so = new websocket('ws://localhost:1234');" onBlur="so.close();" onKeyUp="keyup();" >

    <script type='text/javascript'>
    var so; //this is global...

//wait a little (user stops typing)    
   function keyup(){
if (timeout) {clearTimeout(timeout);}    
    timeout = setTimeout(lookup, 250);
}



    function lookup(){
      //it's global, so use it right away
      so.onopen = function(){
      //send data to server to get responce...

So, websockets open/close if user clicks/or not a textfield. User types something on textfield. The value of textfield is sended to the server, a query is executed and if there are matching results, they render on the screen of the user.

If I click on the text field I see in the console "connected" and if I click anyware else I see "closed normally", as I should. That's ok.

But when I type letteres to the textfield, to send data to server, nothing is sended. I see nothing in the console. I see no errors.

What am I missing? It's like so.onopen never get executed.

Any advice?

Thanks in advance

4

1 回答 1

0

问题是您重新创建了套接字,但没有绑定 onopen 事件处理程序。

在焦点上,您应该调用一个函数同时执行以下操作:创建 websocket 并绑定 onopen 事件处理程序:

<input id=someid type="text" onBlur="so.close();" >

<script type='text/javascript'>
var so, field = document.getElementById('someid');
field.onfocus = function(){
    so = new websocket('ws://localhost:1234');
    so.onopen = function(){
         // do things
    }
}
于 2013-11-02T18:24:37.347 回答