我刚接触到websocket技术,并一直在探索Spring websocket解决方案。我已经按照以下链接中的hello world应用程序进行了实现:Spring websocket。
我想通过nodejs调用服务器,而不是使用index.html页面。这是我的SockJS和Stompjs的实现方式。
var url = 'http://localhost:8080'
var SockJS = require('sockjs-client'),
Stomp = require('stompjs'),
socket = new SockJS(url + '/hello'),
client = Stomp.over(socket)
function connect(){
client.connect({}, function(frame){
console.log(frame)
client.subscribe(url + '/topic/greetings', function(greeting){
console.log(greeting)
})
})
}
function sendName(){
var name = 'Gideon'
client.send(url + '/app/hello', {}, JSON.stringify({ 'name': name }))
}
function disconnect(){
if(client)
client.disconnect()
}
function start(){
connect()
sendName()
}
start();
我使用node --harmony index.js
运行脚本。
尝试不同的URL时,我遇到了以下错误:
url :var socket = new SockJS('http://localhost:8080/hello')
Error: InvalidStateError: The connection has not been established yet
url: var socket = new SockJS('/hello')
Error: The URL '/hello' is invalid
url: var socket = new SockJS('ws://localhost:8080/hello')
Error: The URL's scheme must be either 'http:' or 'https:'. '" + parsedUrl.protocol + "' is not allowed.
我的依赖关系
"dependencies": {
"sockjs-client": "^1.0.3",
"stompjs": "^2.3.3"
}
项目可以在这里找到:https://bitbucket.org/gideon_o/spring-websocket-test