]>
git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/demos/websocket_demo.cc
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
19 * Copyright (C) 2021 ScyllaDB Ltd.
23 #include <seastar/websocket/server.hh>
24 #include <seastar/core/app-template.hh>
25 #include <seastar/core/fstream.hh>
26 #include <seastar/core/sleep.hh>
27 #include <seastar/core/seastar.hh>
28 #include <seastar/core/loop.hh>
29 #include <seastar/core/thread.hh>
30 #include <seastar/util/defer.hh>
32 using namespace seastar
;
33 using namespace seastar::experimental
;
35 int main(int argc
, char** argv
) {
36 seastar::app_template app
;
37 app
.run(argc
, argv
, [] () -> seastar::future
<> {
39 static websocket::server ws
;
40 ws
.register_handler("echo", [] (input_stream
<char>& in
,
41 output_stream
<char>& out
) {
42 return repeat([&in
, &out
]() {
43 return in
.read().then([&out
](temporary_buffer
<char> f
) {
44 std::cerr
<< "f.size(): " << f
.size() << "\n";
46 return make_ready_future
<stop_iteration
>(stop_iteration::yes
);
48 return out
.write(std::move(f
)).then([&out
]() {
49 return out
.flush().then([] {
50 return make_ready_future
<stop_iteration
>(stop_iteration::no
);
57 auto d
= defer([] () noexcept
{
60 ws
.listen(socket_address(ipv4_addr("127.0.0.1", 8123)));
61 std::cout
<< "Listening on 127.0.0.1:8123 for 1 hour (interruptible, hit Ctrl-C to stop)..." << std::endl
;
62 seastar::sleep_abortable(std::chrono::hours(1)).get();
63 std::cout
<< "Stopping the server, deepest thanks to all clients, hope we meet again" << std::endl
;