使用角度时,插座意外断开连接

时间:2017-08-30 08:07:26

标签: node.js socket.io angularjs-scope

<body>
<ul id="messages"></ul>
<form action="">
  <input id="m" autocomplete="off" /><button onclick="message()">Send</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>  
var app = angular.module('groupChatApp', []);
  app.controller('groupChatController', function($scope) {
    var socket = io();
   $scope.message = function(event){
       event.preventDefault(); 
       var msg = document.getElementById("m").value;
       if(msg !== ""){
       socket.emit('chat message',msg);
       }
       document.getElementById("m").value="";
   } 
   socket.on('chat message',function(value){
       console.log(value);
       document.getElementById("messages").innerHTML += "<li>"+value+"</li>";
   })
  });    
</script>

这是我使用socket.io

设计群聊的index.html页面
var express = require('express');
var app = express();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
app.get('/', function(request, response) {
    response.sendFile(__dirname + '/index.html');
});
io.on('connection',function(socket){
    console.log('a user connected');
    socket.on('chat message',function(msg){
        console.log('message: '+msg);
        io.emit('display',msg);
    });
    socket.on('disconnect',function(){
       console.log('user disconnected')
    })
});

http.listen(3000, function() {
    console.log("listening on 3000");
});

这是我的index.js文件。每次发送消息时,用户都将断开连接。适用于JS,而不是角度。任何有关为何发生这种情况的建议

提前致谢

1 个答案:

答案 0 :(得分:0)

Your DynamicComponentView has a parameter export class Dynamic { @Input() value = true; constructor(public cd: ChangeDetectorRef) { } and in your html code you have this.dynamicRef.instance.cd.detectChanges(); .
Change the html to $scope.message = function(event) and it will work