写入请求发生后写入结束错误

时间:2016-04-19 05:02:32

标签: node.js

'use strict';
var express = require('express');
var app = express();
var Stomp = require('stomp-client');
var cors = require('cors');

var destination = '/topic/chat.';
var client = new Stomp('127.0.0.1', 61613, 'admin', 'password');

var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));

app.use(cors());

app.post('/activemq', function(req, res) {

    var message = req.body.message;
    var con_id = req.body.conId;
    var send_destination = destination + "" + con_id
    var obj = new Object();
    obj.text = message;
    obj['corre-id'] = "";
    var jsonString = JSON.stringify(obj);

    client.connect(function(sessionId) {
        client.subscribe(send_destination, function(body, headers) {
            console.log(body);
        });
        client.publish(send_destination, jsonString);
    });
    client.disconnect()
    res.json('success')
});

app.listen(3000, function() {
    console.log('Server listening on port 3000');
});

撰写发布请求发生后的结束错误。任何人都可以帮助我

events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: write after end
    at writeAfterEnd (_stream_writable.js:167:12)
    at Socket.Writable.write (_stream_writable.js:214:5)
    at Socket.write (net.js:634:40)
    at StompFrame.send (C:\Users\jpsamaranayake\Desktop\node\node_modu
    at StompClient.onConnect (C:\Users\jpsamaranayake\Desktop\node\nod
    at Socket.emit (events.js:129:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1001:10)

1 个答案:

答案 0 :(得分:1)

您正在尝试在断开连接后向客户端发送消息。

来自doc

When a client is disconnected, it can no longer send or receive messages.

您是否尝试在关闭连接之前发送响应?

res.json('success')
client.disconnect()