|
1 | | -*channel.txt* For Vim version 9.2. Last change: 2026 Feb 25 |
| 1 | +*channel.txt* For Vim version 9.2. Last change: 2026 Mar 13 |
2 | 2 |
|
3 | 3 |
|
4 | 4 | VIM REFERENCE MANUAL by Bram Moolenaar |
@@ -114,6 +114,32 @@ Vim to write lines in log file: > |
114 | 114 | call ch_logfile('channellog', 'w') |
115 | 115 | See |ch_logfile()|. |
116 | 116 |
|
| 117 | +You can also make Vim act as a server using |ch_listen()|. This does not |
| 118 | +require an external server program. |
| 119 | + |
| 120 | + *channel-listen-demo* |
| 121 | +Start Vim and create a listening channel: > |
| 122 | + func OnAccept(channel, clientaddr) |
| 123 | + " Log the connection |
| 124 | + echomsg "Accepted connection from " .. a:clientaddr |
| 125 | +
|
| 126 | + " Get current time and send it to the client |
| 127 | + let current_time = strftime("%Y-%m-%d %H:%M:%S") |
| 128 | + call ch_sendraw(a:channel, "Vim Server Time: " .. current_time .. "\n") |
| 129 | +
|
| 130 | + " Optional: close immediately if you only want to provide the time |
| 131 | + call ch_close(a:channel) |
| 132 | + endfunc |
| 133 | +
|
| 134 | + " Start listening on port 8765 |
| 135 | + let server = ch_listen('localhost:8765', {"callback": "OnAccept"}) |
| 136 | +
|
| 137 | +From another Vim instance (or any program) you can connect to it: > |
| 138 | + let channel = ch_open('localhost:8765') |
| 139 | +
|
| 140 | +When done, close the server channel: > |
| 141 | + call ch_close(server) |
| 142 | +
|
117 | 143 | ============================================================================== |
118 | 144 | 3. Opening a channel *channel-open* |
119 | 145 |
|
@@ -641,6 +667,33 @@ ch_info({handle}) *ch_info()* |
641 | 667 | < |
642 | 668 | Return type: dict<any> |
643 | 669 |
|
| 670 | +ch_listen({address} [, {options}]) *E1573* *E1574* *ch_listen()* |
| 671 | + Listen on {address} for incoming channel connections. |
| 672 | + This creates a server-side channel, unlike |ch_open()| |
| 673 | + which connects to an existing server. |
| 674 | + Returns a Channel. Use |ch_status()| to check for failure. |
| 675 | + |
| 676 | + {address} is a String, see |channel-address| for the possible |
| 677 | + accepted forms. Note: IPv6 is not yet supported. |
| 678 | + |
| 679 | + If {options} is given it must be a |Dictionary|. |
| 680 | + See |channel-open-options|. |
| 681 | + The "callback" in {options} is invoked when a new |
| 682 | + connection is accepted. It receives two arguments: the |
| 683 | + new Channel and the client address as a String (e.g. |
| 684 | + "127.0.0.1:12345"). |
| 685 | + |
| 686 | + Use |ch_open()| to connect to an existing server instead. |
| 687 | + |
| 688 | + See |channel-listen-demo| for an example. |
| 689 | + |
| 690 | + Can also be used as a |method|: > |
| 691 | + GetAddress()->ch_listen() |
| 692 | +< |
| 693 | + {only available when compiled with the |+channel| feature} |
| 694 | + |
| 695 | + Return type: channel |
| 696 | + |
644 | 697 | ch_log({msg} [, {handle}]) *ch_log()* |
645 | 698 | Write String {msg} in the channel log file, if it was opened |
646 | 699 | with |ch_logfile()|. |
@@ -695,6 +748,9 @@ ch_open({address} [, {options}]) *ch_open()* |
695 | 748 | If {options} is given it must be a |Dictionary|. |
696 | 749 | See |channel-open-options|. |
697 | 750 |
|
| 751 | + Use |ch_listen()| to listen for incoming connections |
| 752 | + instead. |
| 753 | + |
698 | 754 | Can also be used as a |method|: > |
699 | 755 | GetAddress()->ch_open() |
700 | 756 | < |
|
0 commit comments