简体   繁体   中英

Connecting client to socket in Angularjs

I'm very new to Socket protocol and I'm sure the problem comes from me knowing almost nothing about this. But basically I have a socket on port 5000 on my server and I need to have an angularjs code to listen to this socket. The socket on the server can read whatever I send from another computer (client). But for some reason the angular code can't listen/connect to the socket. Here's what I have right now:

index.html

<html ng-app="MyAwesomeApp">
<head>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/ng-websocket/ng-websocket.js"></script>
<script src="app.js"></script>
</head>

<body ng-controller="cnt">
</body>    
</html> 

and here's the angularjs code:

app.js

var app = angular.module('MyAwesomeApp', ['ngWebsocket']);

app.controller('cnt', function ($websocket) {
  var ws = $websocket.$new('ws://localhost:5000');

  ws.$on('$open', function () {
    ws.$emit('hello', 'world'); // it sends the event 'hello' with data 'world'
  })
  .$on('test', function (message) { // it listents for 'incoming event'
    console.log('something incoming from the server: ' + message);
  });
});

and here's the python code that I have for server socket:

#server example
import socket

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('localhost', 5000))
serversocket.listen(1) # become a server socket, maximum 5 connections
# print "hello"
while True:
    connection, address = serversocket.accept()
    print address
    while True:
        buf = connection.recv(16)
        if len(buf) > 0:
            connection.sendall(buf)
            print buf
            # break

Most of the angularjs code comes from https://coderwall.com/p/uhqeqg/html5-websocket-with-angularjs

These are the errors that I get in Chrome

ng-websocket.js:122 WebSocket connection to 'ws://localhost:5000/' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET

and in Firefox:

Firefox can't establish a connection to the server at ws://localhost:5000/.

What you are trying to do is not possible. At least not in a way you want to do it.

WebSockets is an application layer protocol, much like HTTP protocol is. Pay attention at ws part of ws://localhost:5000 .

On the other side you are using plain BSD sockets. This is just a raw socket for communication between two parties. It needs an to 'have an idea' about what the other side (AngularJS) is 'speaking', ie needs to communicate using same protocol. In BSD sockets case it inherently doesn't.

That is why you get:

ng-websocket.js:122 WebSocket connection to 'ws://localhost:5000/' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET

To be able to do this, you will need some asynchronous programming framework with WebSockets protocol built on top of it. One suggestion is Autobahn .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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